• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #include "xfa/fxfa/parser/cxfa_validate.h"
8 
9 #include "fxjs/xfa/cjx_object.h"
10 #include "fxjs/xfa/cjx_validate.h"
11 #include "third_party/base/ptr_util.h"
12 #include "xfa/fxfa/parser/cxfa_message.h"
13 #include "xfa/fxfa/parser/cxfa_picture.h"
14 #include "xfa/fxfa/parser/cxfa_script.h"
15 
16 namespace {
17 
18 const CXFA_Node::PropertyData kPropertyData[] = {{XFA_Element::Message, 1, 0},
19                                                  {XFA_Element::Picture, 1, 0},
20                                                  {XFA_Element::Script, 1, 0},
21                                                  {XFA_Element::Extras, 1, 0},
22                                                  {XFA_Element::Unknown, 0, 0}};
23 const CXFA_Node::AttributeData kAttributeData[] = {
24     {XFA_Attribute::Id, XFA_AttributeType::CData, nullptr},
25     {XFA_Attribute::Use, XFA_AttributeType::CData, nullptr},
26     {XFA_Attribute::ScriptTest, XFA_AttributeType::Enum,
27      (void*)XFA_AttributeEnum::Error},
28     {XFA_Attribute::NullTest, XFA_AttributeType::Enum,
29      (void*)XFA_AttributeEnum::Disabled},
30     {XFA_Attribute::Usehref, XFA_AttributeType::CData, nullptr},
31     {XFA_Attribute::Desc, XFA_AttributeType::CData, nullptr},
32     {XFA_Attribute::FormatTest, XFA_AttributeType::Enum,
33      (void*)XFA_AttributeEnum::Warning},
34     {XFA_Attribute::Lock, XFA_AttributeType::Integer, (void*)0},
35     {XFA_Attribute::Unknown, XFA_AttributeType::Integer, nullptr}};
36 
37 constexpr wchar_t kName[] = L"validate";
38 constexpr wchar_t kFormatTest[] = L"formatTest";
39 constexpr wchar_t kNullTest[] = L"nullTest";
40 constexpr wchar_t kScriptTest[] = L"scriptTest";
41 
42 }  // namespace
43 
CXFA_Validate(CXFA_Document * doc,XFA_PacketType packet)44 CXFA_Validate::CXFA_Validate(CXFA_Document* doc, XFA_PacketType packet)
45     : CXFA_Node(
46           doc,
47           packet,
48           (XFA_XDPPACKET_Config | XFA_XDPPACKET_Template | XFA_XDPPACKET_Form),
49           XFA_ObjectType::ContentNode,
50           XFA_Element::Validate,
51           kPropertyData,
52           kAttributeData,
53           kName,
54           pdfium::MakeUnique<CJX_Validate>(this)) {}
55 
~CXFA_Validate()56 CXFA_Validate::~CXFA_Validate() {}
57 
GetFormatTest()58 XFA_AttributeEnum CXFA_Validate::GetFormatTest() {
59   return JSObject()->GetEnum(XFA_Attribute::FormatTest);
60 }
61 
SetNullTest(const WideString & wsValue)62 void CXFA_Validate::SetNullTest(const WideString& wsValue) {
63   Optional<XFA_AttributeEnum> item =
64       CXFA_Node::NameToAttributeEnum(wsValue.AsStringView());
65   JSObject()->SetEnum(XFA_Attribute::NullTest,
66                       item ? *item : XFA_AttributeEnum::Disabled, false);
67 }
68 
GetNullTest()69 XFA_AttributeEnum CXFA_Validate::GetNullTest() {
70   return JSObject()->GetEnum(XFA_Attribute::NullTest);
71 }
72 
GetScriptTest()73 XFA_AttributeEnum CXFA_Validate::GetScriptTest() {
74   return JSObject()->GetEnum(XFA_Attribute::ScriptTest);
75 }
76 
GetMessageText(const WideString & wsMessageType)77 WideString CXFA_Validate::GetMessageText(const WideString& wsMessageType) {
78   CXFA_Message* pNode =
79       JSObject()->GetProperty<CXFA_Message>(0, XFA_Element::Message);
80   if (!pNode)
81     return L"";
82 
83   for (CXFA_Node* pItemNode = pNode->GetFirstChild(); pItemNode;
84        pItemNode = pItemNode->GetNextSibling()) {
85     if (pItemNode->GetElementType() != XFA_Element::Text)
86       continue;
87 
88     WideString wsName = pItemNode->JSObject()->GetCData(XFA_Attribute::Name);
89     if (wsName.IsEmpty() || wsName == wsMessageType)
90       return pItemNode->JSObject()->GetContent(false);
91   }
92   return L"";
93 }
94 
SetFormatMessageText(const WideString & wsMessage)95 void CXFA_Validate::SetFormatMessageText(const WideString& wsMessage) {
96   SetMessageText(kFormatTest, wsMessage);
97 }
98 
GetFormatMessageText()99 WideString CXFA_Validate::GetFormatMessageText() {
100   return GetMessageText(kFormatTest);
101 }
102 
SetNullMessageText(const WideString & wsMessage)103 void CXFA_Validate::SetNullMessageText(const WideString& wsMessage) {
104   SetMessageText(kNullTest, wsMessage);
105 }
106 
GetNullMessageText()107 WideString CXFA_Validate::GetNullMessageText() {
108   return GetMessageText(kNullTest);
109 }
110 
GetScriptMessageText()111 WideString CXFA_Validate::GetScriptMessageText() {
112   return GetMessageText(kScriptTest);
113 }
114 
SetScriptMessageText(const WideString & wsMessage)115 void CXFA_Validate::SetScriptMessageText(const WideString& wsMessage) {
116   SetMessageText(kScriptTest, wsMessage);
117 }
118 
SetMessageText(const WideString & wsMessageType,const WideString & wsMessage)119 void CXFA_Validate::SetMessageText(const WideString& wsMessageType,
120                                    const WideString& wsMessage) {
121   CXFA_Message* pNode =
122       JSObject()->GetOrCreateProperty<CXFA_Message>(0, XFA_Element::Message);
123   if (!pNode)
124     return;
125 
126   for (CXFA_Node* pItemNode = pNode->GetFirstChild(); pItemNode;
127        pItemNode = pItemNode->GetNextSibling()) {
128     if (pItemNode->GetElementType() != XFA_Element::Text)
129       continue;
130 
131     WideString wsName = pItemNode->JSObject()->GetCData(XFA_Attribute::Name);
132     if (wsName.IsEmpty() || wsName == wsMessageType) {
133       pItemNode->JSObject()->SetContent(wsMessage, wsMessage, false, false,
134                                         true);
135       return;
136     }
137   }
138 
139   CXFA_Node* pTextNode = pNode->CreateSamePacketNode(XFA_Element::Text);
140   pNode->InsertChild(pTextNode, nullptr);
141   pTextNode->JSObject()->SetCData(XFA_Attribute::Name, wsMessageType, false,
142                                   false);
143   pTextNode->JSObject()->SetContent(wsMessage, wsMessage, false, false, true);
144 }
145 
GetPicture()146 WideString CXFA_Validate::GetPicture() {
147   CXFA_Picture* pNode = GetChild<CXFA_Picture>(0, XFA_Element::Picture, false);
148   return pNode ? pNode->JSObject()->GetContent(false) : L"";
149 }
150 
GetScriptIfExists()151 CXFA_Script* CXFA_Validate::GetScriptIfExists() {
152   return GetChild<CXFA_Script>(0, XFA_Element::Script, false);
153 }
154