CLAM-Development  1.4.0
XercesDomWriter.cxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #include "XercesDomWriter.hxx"
23 #include "XercesEncodings.hxx"
24 #include "XercesInitializer.hxx"
25 
26 #include <xercesc/dom/DOMImplementation.hpp>
27 #include <xercesc/dom/DOMImplementationLS.hpp>
28 #include <xercesc/dom/DOMImplementationRegistry.hpp>
29 #include <xercesc/dom/DOMWriter.hpp>
30 #include <xercesc/framework/MemBufFormatTarget.hpp>
31 #include <string>
32 #include <sstream>
33 
34 namespace CLAM
35 {
36  void XercesDomWriter::write(std::ostream & target, xercesc::DOMNode * node)
37  {
39  const XMLCh * propertyCanonical = xercesc::XMLUni::fgDOMWRTCanonicalForm;
40  const XMLCh * propertyPrettyPrint = xercesc::XMLUni::fgDOMWRTFormatPrettyPrint;
41  xercesc::DOMImplementation *impl =
42  xercesc::DOMImplementationRegistry::getDOMImplementation(U("LS"));
43  xercesc::DOMWriter *xercesWriter =
44  ((xercesc::DOMImplementationLS*)impl)->createDOMWriter();
45 
46  if (xercesWriter->canSetFeature(propertyPrettyPrint, mShouldIndent))
47  xercesWriter->setFeature(propertyPrettyPrint, mShouldIndent);
48  if (xercesWriter->canSetFeature(propertyCanonical, mShouldCanonicalize))
49  xercesWriter->setFeature(propertyCanonical, mShouldCanonicalize);
50 
51  xercesc::MemBufFormatTarget * xercesTarget = new xercesc::MemBufFormatTarget();
52  xercesWriter->writeNode(xercesTarget, *node);
53  const char * buffer = (char *) xercesTarget->getRawBuffer();
54  const unsigned bufferLen = xercesTarget->getLen();
55  target << std::string(buffer,bufferLen);
56  delete xercesWriter;
57  delete xercesTarget;
58  }
59 
60 }
61