GME  13
XSAnnotation.cpp
Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (the "License"); you may not use this file except in compliance with
00007  * the License.  You may obtain a copy of the License at
00008  *
00009  *      http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 /*
00019  * $Id: XSAnnotation.cpp 679296 2008-07-24 08:13:42Z borisk $
00020  */
00021 
00022 #include <xercesc/framework/psvi/XSAnnotation.hpp>
00023 #include <xercesc/util/XMLString.hpp>
00024 
00025 #include <xercesc/framework/MemBufInputSource.hpp>
00026 
00027 #include <xercesc/sax2/SAX2XMLReader.hpp>
00028 #include <xercesc/sax2/XMLReaderFactory.hpp>
00029 #include <xercesc/parsers/XercesDOMParser.hpp>
00030 #include <xercesc/dom/DOMElement.hpp>
00031 
00032 XERCES_CPP_NAMESPACE_BEGIN
00033 
00034 XSAnnotation::XSAnnotation(const XMLCh*          const content,
00035                                  MemoryManager * const manager)
00036 :XSObject(XSConstants::ANNOTATION, 0, manager)
00037 ,fContents(XMLString::replicate(content, manager))
00038 ,fNext(0)
00039 ,fSystemId(0)
00040 ,fLine(0)
00041 ,fCol(0)
00042 {
00043 }
00044 
00045 XSAnnotation::XSAnnotation(MemoryManager * const manager)
00046 :XSObject(XSConstants::ANNOTATION, 0, manager)
00047 ,fContents(0)
00048 ,fNext(0)
00049 ,fSystemId(0)
00050 ,fLine(0)
00051 ,fCol(0)
00052 {
00053 }
00054 
00055 XSAnnotation::~XSAnnotation()
00056 {
00057     fMemoryManager->deallocate(fContents);
00058 
00059     if (fNext)
00060         delete fNext;
00061 
00062     fMemoryManager->deallocate(fSystemId);
00063 }
00064 
00065 // XSAnnotation methods
00066 void XSAnnotation::writeAnnotation(DOMNode* node, ANNOTATION_TARGET targetType)
00067 {
00068     XercesDOMParser *parser = new (fMemoryManager) XercesDOMParser(0, fMemoryManager);
00069     parser->setDoNamespaces(true);
00070     parser->setValidationScheme(XercesDOMParser::Val_Never);
00071 
00072     DOMDocument* futureOwner = (targetType == W3C_DOM_ELEMENT) ?
00073         ((DOMElement*)node)->getOwnerDocument() :
00074         (DOMDocument*)node;
00075 
00076     MemBufInputSource* memBufIS = new (fMemoryManager) MemBufInputSource
00077     (
00078         (const XMLByte*)fContents
00079         , XMLString::stringLen(fContents)*sizeof(XMLCh)
00080         , ""
00081         , false
00082         , fMemoryManager
00083     );
00084     memBufIS->setEncoding(XMLUni::fgXMLChEncodingString);
00085     memBufIS->setCopyBufToStream(false);
00086 
00087     try
00088     {
00089         parser->parse(*memBufIS);
00090     }
00091     catch (const XMLException&)
00092     {
00093         // REVISIT:  should we really eat this?
00094     }
00095 
00096     DOMNode* newElem = futureOwner->importNode((parser->getDocument())->getDocumentElement(), true);
00097     node->insertBefore(newElem, node->getFirstChild());
00098 
00099     delete parser;
00100     delete memBufIS;
00101 }
00102 
00103 
00104 void XSAnnotation::writeAnnotation(ContentHandler* handler)
00105 {
00106     SAX2XMLReader* parser = XMLReaderFactory::createXMLReader(fMemoryManager);
00107     parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, true);
00108     parser->setFeature(XMLUni::fgSAX2CoreValidation, false);
00109     parser->setContentHandler(handler);
00110 
00111     MemBufInputSource* memBufIS = new (fMemoryManager) MemBufInputSource
00112     (
00113         (const XMLByte*)fContents
00114         , XMLString::stringLen(fContents)*sizeof(XMLCh)
00115         , ""
00116         , false
00117         , fMemoryManager
00118     );
00119     memBufIS->setEncoding(XMLUni::fgXMLChEncodingString);
00120     memBufIS->setCopyBufToStream(false);
00121 
00122     try
00123     {
00124         parser->parse(*memBufIS);
00125     }
00126     catch (const XMLException&)
00127     {
00128     }
00129 
00130     delete parser;
00131     delete memBufIS;
00132 }
00133 
00134 
00135 void XSAnnotation::setNext(XSAnnotation* const nextAnnotation)
00136 {
00137     if (fNext)
00138         fNext->setNext(nextAnnotation);
00139     else
00140         fNext = nextAnnotation;
00141 }
00142 
00143 XSAnnotation* XSAnnotation::getNext()
00144 {
00145     return fNext;
00146 }
00147 
00148 void XSAnnotation::setSystemId(const XMLCh* const systemId)
00149 {
00150     if (fSystemId)
00151     {
00152         fMemoryManager->deallocate(fSystemId);
00153         fSystemId = 0;
00154     }
00155 
00156     if (systemId)
00157         fSystemId = XMLString::replicate(systemId, fMemoryManager);
00158 
00159 }
00160 
00161 /***
00162  * Support for Serialization/De-serialization
00163  ***/
00164 
00165 IMPL_XSERIALIZABLE_TOCREATE(XSAnnotation)
00166 
00167 void XSAnnotation::serialize(XSerializeEngine& serEng)
00168 {
00169 
00170     if (serEng.isStoring())
00171     {
00172         serEng.writeString(fContents);
00173         serEng<<fNext;
00174         serEng.writeString(fSystemId);
00175 
00176         serEng.writeUInt64 (fLine);
00177         serEng.writeUInt64 (fCol);
00178     }
00179     else
00180     {
00181         serEng.readString(fContents);
00182         serEng>>fNext;
00183         serEng.readString(fSystemId);
00184 
00185         serEng.readUInt64 (fLine);
00186         serEng.readUInt64 (fCol);
00187     }
00188 }
00189 
00190 XERCES_CPP_NAMESPACE_END