PageRenderTime 61ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/org.mwc.asset.comms/docs/restlet_src/org.restlet.ext.xml/org/restlet/ext/xml/SaxRepresentation.java

https://bitbucket.org/haris_peco/debrief
Java | 241 lines | 108 code | 22 blank | 111 comment | 11 complexity | 667af97d09b16c41a5374e62cc35e484 MD5 | raw file
  1. /**
  2. * Copyright 2005-2010 Noelios Technologies.
  3. *
  4. * The contents of this file are subject to the terms of one of the following
  5. * open source licenses: LGPL 3.0 or LGPL 2.1 or CDDL 1.0 or EPL 1.0 (the
  6. * "Licenses"). You can select the license that you prefer but you may not use
  7. * this file except in compliance with one of these Licenses.
  8. *
  9. * You can obtain a copy of the LGPL 3.0 license at
  10. * http://www.opensource.org/licenses/lgpl-3.0.html
  11. *
  12. * You can obtain a copy of the LGPL 2.1 license at
  13. * http://www.opensource.org/licenses/lgpl-2.1.php
  14. *
  15. * You can obtain a copy of the CDDL 1.0 license at
  16. * http://www.opensource.org/licenses/cddl1.php
  17. *
  18. * You can obtain a copy of the EPL 1.0 license at
  19. * http://www.opensource.org/licenses/eclipse-1.0.php
  20. *
  21. * See the Licenses for the specific language governing permissions and
  22. * limitations under the Licenses.
  23. *
  24. * Alternatively, you can obtain a royalty free commercial license with less
  25. * limitations, transferable or non-transferable, directly at
  26. * http://www.noelios.com/products/restlet-engine
  27. *
  28. * Restlet is a registered trademark of Noelios Technologies.
  29. */
  30. package org.restlet.ext.xml;
  31. import java.io.IOException;
  32. import java.io.Writer;
  33. import javax.xml.transform.Result;
  34. import javax.xml.transform.TransformerConfigurationException;
  35. import javax.xml.transform.TransformerException;
  36. import javax.xml.transform.TransformerFactory;
  37. import javax.xml.transform.TransformerFactoryConfigurationError;
  38. import javax.xml.transform.dom.DOMSource;
  39. import javax.xml.transform.sax.SAXResult;
  40. import javax.xml.transform.sax.SAXSource;
  41. import org.restlet.data.MediaType;
  42. import org.restlet.representation.Representation;
  43. import org.w3c.dom.Document;
  44. import org.xml.sax.ContentHandler;
  45. import org.xml.sax.InputSource;
  46. /**
  47. * XML representation for SAX events processing. The purpose is to create a
  48. * streamable content based on a custom Java object model instead of a neutral
  49. * DOM tree. This domain object can then be directly modified and efficiently
  50. * serialized at a later time.<br>
  51. * <br>
  52. * Subclasses only need to override the ContentHandler methods required for the
  53. * reading and also the write(XmlWriter writer) method when serialization is
  54. * requested.
  55. *
  56. * @author Jerome Louvel
  57. */
  58. public class SaxRepresentation extends XmlRepresentation {
  59. /** The SAX source. */
  60. private volatile SAXSource source;
  61. /**
  62. * Default constructor. Uses the {@link MediaType#TEXT_XML} media type.
  63. */
  64. public SaxRepresentation() {
  65. this(MediaType.TEXT_XML);
  66. }
  67. /**
  68. * Constructor.
  69. *
  70. * @param mediaType
  71. * The representation media type.
  72. */
  73. public SaxRepresentation(MediaType mediaType) {
  74. super(mediaType);
  75. }
  76. /**
  77. * Constructor.
  78. *
  79. * @param mediaType
  80. * The representation's media type.
  81. * @param xmlDocument
  82. * A DOM document to parse.
  83. */
  84. public SaxRepresentation(MediaType mediaType, Document xmlDocument) {
  85. super(mediaType);
  86. this.source = new SAXSource(SAXSource
  87. .sourceToInputSource(new DOMSource(xmlDocument)));
  88. }
  89. /**
  90. * Constructor.
  91. *
  92. * @param mediaType
  93. * The representation's media type.
  94. * @param xmlSource
  95. * A SAX input source to parse.
  96. */
  97. public SaxRepresentation(MediaType mediaType, InputSource xmlSource) {
  98. super(mediaType);
  99. this.source = new SAXSource(xmlSource);
  100. }
  101. /**
  102. * Constructor.
  103. *
  104. * @param mediaType
  105. * The representation's media type.
  106. * @param xmlSource
  107. * A JAXP source to parse.
  108. */
  109. public SaxRepresentation(MediaType mediaType, SAXSource xmlSource) {
  110. super(mediaType);
  111. this.source = xmlSource;
  112. }
  113. /**
  114. * Constructor.
  115. *
  116. * @param xmlRepresentation
  117. * A source XML representation to parse.
  118. * @throws IOException
  119. */
  120. public SaxRepresentation(Representation xmlRepresentation) {
  121. super((xmlRepresentation == null) ? null : xmlRepresentation
  122. .getMediaType());
  123. try {
  124. if (xmlRepresentation instanceof XmlRepresentation) {
  125. this.source = ((XmlRepresentation) xmlRepresentation)
  126. .getSaxSource();
  127. } else {
  128. this.source = new SAXSource(new InputSource(xmlRepresentation
  129. .getReader()));
  130. }
  131. if (xmlRepresentation.getLocationRef() != null) {
  132. this.source.setSystemId(xmlRepresentation.getLocationRef()
  133. .getTargetRef().toString());
  134. }
  135. } catch (IOException ioe) {
  136. ioe.printStackTrace();
  137. }
  138. }
  139. @Override
  140. public InputSource getInputSource() throws IOException {
  141. return (getSaxSource() == null) ? null : getSaxSource()
  142. .getInputSource();
  143. }
  144. /**
  145. * Returns the SAX source that can be parsed by the
  146. * {@link #parse(ContentHandler)} method or used for an XSLT transformation.
  147. */
  148. @Override
  149. public SAXSource getSaxSource() throws IOException {
  150. return this.source;
  151. }
  152. /**
  153. * Parses the source and sends SAX events to a content handler.
  154. *
  155. * @param contentHandler
  156. * The SAX content handler to use for parsing.
  157. */
  158. public void parse(ContentHandler contentHandler) throws IOException {
  159. if (contentHandler != null) {
  160. try {
  161. Result result = new SAXResult(contentHandler);
  162. TransformerFactory.newInstance().newTransformer().transform(
  163. this.source, result);
  164. } catch (TransformerConfigurationException tce) {
  165. throw new IOException(
  166. "Couldn't parse the source representation: "
  167. + tce.getMessage());
  168. } catch (TransformerException te) {
  169. te.printStackTrace();
  170. throw new IOException(
  171. "Couldn't parse the source representation: "
  172. + te.getMessage());
  173. } catch (TransformerFactoryConfigurationError tfce) {
  174. throw new IOException(
  175. "Couldn't parse the source representation: "
  176. + tfce.getMessage());
  177. }
  178. } else {
  179. throw new IOException(
  180. "Couldn't parse the source representation: no content restlet defined.");
  181. }
  182. }
  183. /**
  184. * Releases the namespaces map.
  185. */
  186. @Override
  187. public void release() {
  188. if (this.source != null) {
  189. this.source = null;
  190. }
  191. super.release();
  192. }
  193. /**
  194. * Sets a SAX source that can be parsed by the
  195. * {@link #parse(ContentHandler)} method.
  196. *
  197. * @param source
  198. * A SAX source.
  199. */
  200. public void setSaxSource(SAXSource source) {
  201. this.source = source;
  202. }
  203. @Override
  204. public void write(Writer writer) throws IOException {
  205. XmlWriter xmlWriter = new XmlWriter(writer);
  206. write(xmlWriter);
  207. }
  208. /**
  209. * Writes the representation to a XML writer. The default implementation
  210. * does nothing and is intended to be overridden.
  211. *
  212. * @param writer
  213. * The XML writer to write to.
  214. * @throws IOException
  215. */
  216. public void write(XmlWriter writer) throws IOException {
  217. // Do nothing by default.
  218. }
  219. }