source: icXML/icXML-devel/src/xercesc/framework/psvi/XSAnnotation.cpp @ 2722

Last change on this file since 2722 was 2722, checked in by cameron, 6 years ago

Original Xerces files with import mods for icxercesc

File size: 4.7 KB
Line 
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/*
19 * $Id: XSAnnotation.cpp 679296 2008-07-24 08:13:42Z borisk $
20 */
21
22#include <xercesc/framework/psvi/XSAnnotation.hpp>
23#include <icxercesc/util/XMLString.hpp>
24
25#include <xercesc/framework/MemBufInputSource.hpp>
26
27#include <xercesc/sax2/SAX2XMLReader.hpp>
28#include <xercesc/sax2/XMLReaderFactory.hpp>
29#include <xercesc/parsers/XercesDOMParser.hpp>
30#include <xercesc/dom/DOMElement.hpp>
31
32XERCES_CPP_NAMESPACE_BEGIN
33
34XSAnnotation::XSAnnotation(const XMLCh*          const content,
35                                 MemoryManager * const manager)
36:XSObject(XSConstants::ANNOTATION, 0, manager)
37,fContents(XMLString::replicate(content, manager))
38,fNext(0)
39,fSystemId(0)
40,fLine(0)
41,fCol(0)
42{
43}
44
45XSAnnotation::XSAnnotation(MemoryManager * const manager)
46:XSObject(XSConstants::ANNOTATION, 0, manager)
47,fContents(0)
48,fNext(0)
49,fSystemId(0)
50,fLine(0)
51,fCol(0)
52{
53}
54
55XSAnnotation::~XSAnnotation()
56{
57    fMemoryManager->deallocate(fContents);
58
59    if (fNext)
60        delete fNext;
61
62    fMemoryManager->deallocate(fSystemId);
63}
64
65// XSAnnotation methods
66void XSAnnotation::writeAnnotation(DOMNode* node, ANNOTATION_TARGET targetType)
67{
68    XercesDOMParser *parser = new (fMemoryManager) XercesDOMParser(0, fMemoryManager);
69    parser->setDoNamespaces(true);
70    parser->setValidationScheme(XercesDOMParser::Val_Never);
71
72    DOMDocument* futureOwner = (targetType == W3C_DOM_ELEMENT) ?
73        ((DOMElement*)node)->getOwnerDocument() :
74        (DOMDocument*)node;
75
76    MemBufInputSource* memBufIS = new (fMemoryManager) MemBufInputSource
77    (
78        (const XMLByte*)fContents
79        , XMLString::stringLen(fContents)*sizeof(XMLCh)
80        , ""
81        , false
82        , fMemoryManager
83    );
84    memBufIS->setEncoding(XMLUni::fgXMLChEncodingString);
85    memBufIS->setCopyBufToStream(false);
86
87    try
88    {
89        parser->parse(*memBufIS);
90    }
91    catch (const XMLException&)
92    {
93        // REVISIT:  should we really eat this?
94    }
95
96    DOMNode* newElem = futureOwner->importNode((parser->getDocument())->getDocumentElement(), true);
97    node->insertBefore(newElem, node->getFirstChild());
98
99    delete parser;
100    delete memBufIS;
101}
102
103
104void XSAnnotation::writeAnnotation(ContentHandler* handler)
105{
106    SAX2XMLReader* parser = XMLReaderFactory::createXMLReader(fMemoryManager);
107    parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, true);
108    parser->setFeature(XMLUni::fgSAX2CoreValidation, false);
109    parser->setContentHandler(handler);
110
111    MemBufInputSource* memBufIS = new (fMemoryManager) MemBufInputSource
112    (
113        (const XMLByte*)fContents
114        , XMLString::stringLen(fContents)*sizeof(XMLCh)
115        , ""
116        , false
117        , fMemoryManager
118    );
119    memBufIS->setEncoding(XMLUni::fgXMLChEncodingString);
120    memBufIS->setCopyBufToStream(false);
121
122    try
123    {
124        parser->parse(*memBufIS);
125    }
126    catch (const XMLException&)
127    {
128    }
129
130    delete parser;
131    delete memBufIS;
132}
133
134
135void XSAnnotation::setNext(XSAnnotation* const nextAnnotation)
136{
137    if (fNext)
138        fNext->setNext(nextAnnotation);
139    else
140        fNext = nextAnnotation;
141}
142
143XSAnnotation* XSAnnotation::getNext()
144{
145    return fNext;
146}
147
148void XSAnnotation::setSystemId(const XMLCh* const systemId)
149{
150    if (fSystemId)
151    {
152        fMemoryManager->deallocate(fSystemId);
153        fSystemId = 0;
154    }
155
156    if (systemId)
157        fSystemId = XMLString::replicate(systemId, fMemoryManager);
158
159}
160
161/***
162 * Support for Serialization/De-serialization
163 ***/
164
165IMPL_XSERIALIZABLE_TOCREATE(XSAnnotation)
166
167void XSAnnotation::serialize(XSerializeEngine& serEng)
168{
169
170    if (serEng.isStoring())
171    {
172        serEng.writeString(fContents);
173        serEng<<fNext;
174        serEng.writeString(fSystemId);
175
176        serEng.writeUInt64 (fLine);
177        serEng.writeUInt64 (fCol);
178    }
179    else
180    {
181        serEng.readString(fContents);
182        serEng>>fNext;
183        serEng.readString(fSystemId);
184
185        serEng.readUInt64 (fLine);
186        serEng.readUInt64 (fCol);
187    }
188}
189
190XERCES_CPP_NAMESPACE_END
Note: See TracBrowser for help on using the repository browser.