1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef PSVIWRITERHANDLER_HPP
19 #define PSVIWRITERHANDLER_HPP
20 
21 // ---------------------------------------------------------------------------
22 //  Includes
23 // ---------------------------------------------------------------------------
24 #include <xercesc/sax2/Attributes.hpp>
25 #include <xercesc/sax2/DefaultHandler.hpp>
26 #include <xercesc/framework/psvi/XSConstants.hpp>
27 #include <xercesc/framework/psvi/PSVIHandler.hpp>
28 #include <xercesc/framework/psvi/PSVIAttribute.hpp>
29 #include <xercesc/framework/psvi/PSVIAttributeList.hpp>
30 #include <xercesc/framework/psvi/PSVIElement.hpp>
31 #include <xercesc/framework/psvi/PSVIItem.hpp>
32 #include <xercesc/framework/psvi/XSAnnotation.hpp>
33 #include <xercesc/framework/psvi/XSAttributeDeclaration.hpp>
34 #include <xercesc/framework/psvi/XSAttributeGroupDefinition.hpp>
35 #include <xercesc/framework/psvi/XSAttributeUse.hpp>
36 #include <xercesc/framework/psvi/XSComplexTypeDefinition.hpp>
37 #include <xercesc/framework/psvi/XSElementDeclaration.hpp>
38 #include <xercesc/framework/psvi/XSFacet.hpp>
39 #include <xercesc/framework/psvi/XSIDCDefinition.hpp>
40 #include <xercesc/framework/psvi/XSModel.hpp>
41 #include <xercesc/framework/psvi/XSModelGroup.hpp>
42 #include <xercesc/framework/psvi/XSModelGroupDefinition.hpp>
43 #include <xercesc/framework/psvi/XSMultiValueFacet.hpp>
44 #include <xercesc/framework/psvi/XSNamedMap.hpp>
45 #include <xercesc/framework/psvi/XSNamespaceItem.hpp>
46 #include <xercesc/framework/psvi/XSNotationDeclaration.hpp>
47 #include <xercesc/framework/psvi/XSParticle.hpp>
48 #include <xercesc/framework/psvi/XSSimpleTypeDefinition.hpp>
49 #include <xercesc/framework/psvi/XSTypeDefinition.hpp>
50 #include <xercesc/framework/psvi/XSWildcard.hpp>
51 #include <xercesc/framework/XMLFormatter.hpp>
52 #include <xercesc/framework/XMLDocumentHandler.hpp>
53 #include <xercesc/dom/DOMElement.hpp>
54 #include <xercesc/dom/DOMNamedNodeMap.hpp>
55 #include <xercesc/util/ValueStackOf.hpp>
56 #include <xercesc/util/ValueVectorOf.hpp>
57 #include <xercesc/util/XMLEntityResolver.hpp>
58 #include <xercesc/util/XMLResourceIdentifier.hpp>
59 #include <stdlib.h>
60 #include <string.h>
61 
62 XERCES_CPP_NAMESPACE_USE
63 
64 
65 class AttrInfo {
66 public:
AttrInfo(const XMLCh * pUri,const XMLCh * pName,const XMLCh * pType,const XMLCh * pValue)67 	AttrInfo(const XMLCh* pUri, const XMLCh* pName, const XMLCh* pType, const XMLCh* pValue) {
68 		uri = XMLString::replicate(pUri);
69 		name = XMLString::replicate(pName);
70 		type = XMLString::replicate(pType);
71 		value = XMLString::replicate(pValue);
72 	}
73 
~AttrInfo()74 	~AttrInfo() {
75 		XMLString::release((XMLCh**)&uri);
76 		XMLString::release((XMLCh**)&name);
77 		XMLString::release((XMLCh**)&type);
78 		XMLString::release((XMLCh**)&value);
79 	}
80 
getUri() const81 	const XMLCh* getUri() const {
82 		return uri;
83 	}
84 
getLocalName() const85 	const XMLCh* getLocalName() const {
86 		return name;
87 	}
88 
getType() const89 	const XMLCh* getType() const {
90 		return type;
91 	}
92 
getValue() const93 	const XMLCh* getValue() const {
94 		return value;
95 	}
96 
97 private:
98 	const XMLCh* uri;
99 	const XMLCh* name;
100 	const XMLCh* type;
101 	const XMLCh* value;
102 };
103 
104 class PSVIWriterHandlers : public PSVIHandler, public DefaultHandler, public XMLEntityResolver {
105 public:
106     // -----------------------------------------------------------------------
107     //  Constructors and Destructor
108     // -----------------------------------------------------------------------
109     PSVIWriterHandlers(XMLFormatter* outputFormatter, XMLFormatter* errorFormatter = NULL);
110     ~PSVIWriterHandlers();
111 
112     friend class PSVIAdvancedHandler;
113     // -----------------------------------------------------------------------
114     //  Convenience Utility
115     // -----------------------------------------------------------------------
116 	void resetPSVIFormatter(XMLFormatter* outputFormatter);
117     void resetDocument();
118 
119     // -----------------------------------------------------------------------
120     //  Handlers for the SAX ContentHandler interface
121     // -----------------------------------------------------------------------
122     void startElement(const XMLCh* const uri, const XMLCh* const localname, const XMLCh* const qname, const Attributes& attrs);
123     void endElement(const XMLCh* const uri, const XMLCh* const localname, const XMLCh* const qname);
124     void startDocument();
125     void endDocument();
126     void characters(const XMLCh* const chars, const XMLSize_t length);
127     void ignorableWhitespace(const XMLCh* const chars, const XMLSize_t length);
128     void comment(const XMLCh* const chars, const XMLSize_t length);
129     void processingInstruction(const XMLCh* const target, const XMLCh* const data);
130     void startPrefixMapping(const XMLCh* const prefix, const XMLCh* const uri);
131     void endPrefixMapping(const XMLCh* const prefix);
132     InputSource* resolveEntity(XMLResourceIdentifier* resourceIdentifier);
133     InputSource* resolveEntity(const XMLCh* const publicId, const XMLCh* const systemId);
134 
135     // -----------------------------------------------------------------------
136     //  Handlers for the SAX ErrorHandler interface
137     // -----------------------------------------------------------------------
138 	void warning(const SAXParseException& exc);
139     void error(const SAXParseException& exc);
140     void fatalError(const SAXParseException& exc);
141     void resetErrors();
142 
143     // -----------------------------------------------------------------------
144     //  Handlers for the PSVIHandler interface
145     // -----------------------------------------------------------------------
146 
147 	void handleAttributesPSVI( const XMLCh* const localName,
148 								const XMLCh* const uri,
149 								PSVIAttributeList* psviAttributes );
150 	void handleElementPSVI(	const XMLCh* const localName,
151                                 const XMLCh* const uri,
152                                 PSVIElement* elementInfo );
153 	void handlePartialElementPSVI( const XMLCh* const localName,
154                                    const XMLCh* const uri,
155                                    PSVIElement* elementInfo );
156 private:
157     // -----------------------------------------------------------------------
158     //  Private methods
159     // -----------------------------------------------------------------------
160 
161     void processAttributes(PSVIAttributeList* psviAttributes, const RefVectorOf<AttrInfo>* attributesInfo);
162     void processNamespaceAttributes(PSVIAttributeList* psviAttributes, const RefVectorOf<AttrInfo>* attributes);
163     void processAttributePSVI(PSVIAttribute* attribute);
164     void processInScopeNamespaces();
165     void processActualValue(PSVIItem*);
166     void formDateTime(XSValue*);
167 
168     void processSchemaInformation(XSModel* model);
169     void processNamespaceItem(XSNamespaceItem* namespaceItem);
170     void processSchemaComponents(XSNamespaceItem* namespaceItem);
171     void processSchemaDocuments(XSNamespaceItem* namespaceItem);
172     void processSchemaAnnotations(XSAnnotationList* annotations);
173     void processSchemaErrorCode(StringList* errors);
174 
175     void processTypeDefinition(XSTypeDefinition* type);
176     void processComplexTypeDefinition(XSComplexTypeDefinition* complexType);
177     void processSimpleTypeDefinition(XSSimpleTypeDefinition* simpleType);
178     void processModelGroupDefinition(XSModelGroupDefinition* modelGroup);
179     void processAttributeGroupDefinition(XSAttributeGroupDefinition* attributeGroup);
180 
181     void processElementDeclaration(XSElementDeclaration* element);
182     void processAttributeDeclaration(XSAttributeDeclaration* attribute);
183     void processNotationDeclaration(XSNotationDeclaration* notation);
184 
185     void processAnnotations(XSAnnotationList* annotations);
186     void processAttributeUses(XSAttributeUseList* attributeUses);
187     void processFacets(XSFacetList* facets, XSMultiValueFacetList* multiFacets);
188     void processFundamentalFacets(XSSimpleTypeDefinition* facets);
189     void processMemberTypeDefinitions(XSSimpleTypeDefinitionList* memberTypes);
190 
191     void processAnnotation(XSAnnotation* annotation);
192     void processDOMElement(const XMLCh* const encloseName, DOMElement* rootElem, const XMLCh* const elementName);
193     void processDOMAttributes(DOMNamedNodeMap* attrs);
194     void processWildcard(XSWildcard* wildcard);
195     void processModelGroup(XSModelGroup* modelGroup);
196     void processParticle(XSParticle* particle);
197 
198     void processAttributeWildcard(XSWildcard* wildcard);
199     void processScope(XSComplexTypeDefinition* enclosingCTD, short scope);
200     void processValueConstraint(XSConstants::VALUE_CONSTRAINT ConstraintType, const XMLCh* constraintValue);
201 
202     void processIdentityConstraintDefinition(XSNamedMap<XSIDCDefinition>* identityConstraint);
203     void processFields(StringList* fields);
204     void processXPath(const XMLCh* xpath);
205 
206     void processChildren();
207     void processChildrenEnd();
208 
209     void processTypeDefinitionOrRef(const XMLCh* enclose, XSTypeDefinition* type);
210 	void processSimpleTypeDefinitionOrRef(XSSimpleTypeDefinition* type);
211     void processAttributeDeclarationOrRef(XSAttributeDeclaration* attrDecl);
212     void processElementDeclarationOrRef(XSElementDeclaration* elemDecl);
213 	void processTypeDefinitionRef(const XMLCh* enclose, XSTypeDefinition* type);
214     void processAttributeDeclarationRef(const XMLCh* enclose, XSAttributeDeclaration* attrDecl);
215     void processElementDeclarationRef(const XMLCh* enclose, XSElementDeclaration* elemDecl);
216     void sendReference(const XMLCh* elementName, XSObject* obj);
217 
218     void sendElementEmpty(const XMLCh* const elementName);
219 	void sendElementValueInt(const XMLCh* const elementName, int value);
220     void sendElementValue(const XMLCh* const elementName, const XMLCh* const value);
221     void sendElementValueList(const XMLCh* const elementName, const StringList* const values);
222 
223 	void sendIndentedElement(const XMLCh* const elementName);
224     void sendIndentedElementWithID(const XMLCh* const elementName, XSObject* obj);	//adds the ID to the attribute list before sending
225     void sendUnindentedElement(const XMLCh* const elementName);
226 
227     void writeOpen(const XMLCh* const elementName);
228 	void writeOpen(const XMLCh* const elementName, const StringList* const attrs);
229 	void writeClose(const XMLCh* const elementName);
230 	void writeValue(const XMLCh* const elementName, const XMLCh* const value);
231 	void writeValue(const XMLCh* const elementName, const StringList* const values);
232 	void writeEmpty(const XMLCh* const elementName, const StringList* const attrs);
233 	void writeEmpty(const XMLCh* const elementName);
234     void writeString(const XMLCh* const string);
235 
236     const XMLCh* translateScope(XSConstants::SCOPE scope);
237     const XMLCh* translateValueConstraint(XSConstants::VALUE_CONSTRAINT constraintKind);
238     const XMLCh* translateBlockOrFinal(short val);
239     const XMLCh* translateDerivationMethod(XSConstants::DERIVATION_TYPE derivation);
240     const XMLCh* translateProcessContents(XSWildcard::PROCESS_CONTENTS processContents);
241     const XMLCh* translateCompositor(XSModelGroup::COMPOSITOR_TYPE compositor);
242     const XMLCh* translateValidity(PSVIItem::VALIDITY_STATE validity);
243     const XMLCh* translateValidationAttempted(PSVIItem::ASSESSMENT_TYPE validation);
244     const XMLCh* translateIdConstraintCategory(XSIDCDefinition::IC_CATEGORY category);
245     const XMLCh* translateComplexContentType(XSComplexTypeDefinition::CONTENT_TYPE contentType);
246     const XMLCh* translateSimpleTypeVariety(XSSimpleTypeDefinition::VARIETY variety);
247     const XMLCh* translateOrderedFacet(XSSimpleTypeDefinition::ORDERING ordered);
248     const XMLCh* translateFacet(XSSimpleTypeDefinition::FACET facetKind);
249     const XMLCh* translateComponentType(XSConstants::COMPONENT_TYPE type);
250     const XMLCh* translateBool(bool flag);
251 
252     XMLCh* createID(XSObject* obj);
253     const XMLCh* getIdName(XSObject* obj);
254     void incIndent();
255     void decIndent();
256 
257 protected:
258 	XMLFormatter* fFormatter;
259 	XMLFormatter* fErrorFormatter;
260 
261 	StringList* fAttrList;
262     XMLCh* fTempResult;
263     XMLCh* fIndentChars;
264     XMLCh* fBaseUri;
265 
266     unsigned int fIndent;
267     unsigned int fIndentCap;
268     unsigned int fAnonNum;
269 
270 	RefHashTableOf<XMLCh>* fIdMap;
271     RefVectorOf<XSObject>* fDefinedIds;
272     RefArrayVectorOf<XMLCh>* fIdNames;
273     RefArrayVectorOf<XMLCh>* fObjectLocations;
274 
275 	RefHashTableOf<XMLCh>* fPrefixMap;
276     RefArrayVectorOf<XMLCh>* fNamespaces;
277 
278 	ValueVectorOf<XMLSize_t>* fNSAttributes;  //REVISIT  dont need if NSAttrs in different object
279 	ValueStackOf<bool>* fElementChildren;
280 
281 	RefVectorOf<AttrInfo>* fAttributesInfo;
282 };
283 
284 class PSVIAdvancedHandler: public XMLDocumentHandler {
285 public:
PSVIAdvancedHandler(PSVIWriterHandlers * writerHandler)286     PSVIAdvancedHandler(PSVIWriterHandlers* writerHandler) : XMLDocumentHandler(), fWriterHandler(writerHandler) {}
~PSVIAdvancedHandler()287     ~PSVIAdvancedHandler() {}
docCharacters(const XMLCh * const,const XMLSize_t,const bool)288     void docCharacters(const XMLCh* const, const XMLSize_t, const bool) {}
docComment(const XMLCh * const)289     void docComment(const XMLCh* const) {}
docPI(const XMLCh * const,const XMLCh * const)290     void docPI(const XMLCh* const, const XMLCh* const) {}
endDocument()291     void endDocument() {}
endElement(const XMLElementDecl &,const unsigned int,const bool,const XMLCh * const)292     void endElement(const XMLElementDecl&, const unsigned int, const bool, const XMLCh* const) {}
endEntityReference(const XMLEntityDecl &)293     void endEntityReference(const   XMLEntityDecl&) {}
294 
ignorableWhitespace(const XMLCh * const,const XMLSize_t,const bool)295     void ignorableWhitespace(const XMLCh* const, const XMLSize_t, const bool) {}
296 
resetDocument()297     void resetDocument() {}
startDocument()298     void startDocument() {}
startElement(const XMLElementDecl &,const unsigned int,const XMLCh * const,const RefVectorOf<XMLAttr> &,const XMLSize_t,const bool,const bool)299     void startElement(const XMLElementDecl&, const unsigned int, const XMLCh* const, const RefVectorOf<XMLAttr>&
300                      ,const XMLSize_t, const bool, const bool) {}
startEntityReference(const XMLEntityDecl &)301     void startEntityReference(const XMLEntityDecl&) {};
302 
303     void XMLDecl(const XMLCh* const versionStr, const XMLCh* const encodingStr, const XMLCh* const standaloneStr, const XMLCh* const autoEncodingStr);
304 private:
305     PSVIWriterHandlers* fWriterHandler;
306 };
307 
308 #endif
309