1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xerces" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, International
  53. * Business Machines, Inc., http://www.apache.org. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package com.sun.org.apache.xerces.internal.impl.xs.dom;
  58. import com.sun.org.apache.xerces.internal.parsers.NonValidatingConfiguration;
  59. import com.sun.org.apache.xerces.internal.impl.Constants;
  60. import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter;
  61. import com.sun.org.apache.xerces.internal.impl.xs.SchemaSymbols;
  62. import com.sun.org.apache.xerces.internal.impl.xs.XSMessageFormatter;
  63. import com.sun.org.apache.xerces.internal.xni.XMLLocator;
  64. import com.sun.org.apache.xerces.internal.xni.NamespaceContext;
  65. import com.sun.org.apache.xerces.internal.xni.QName;
  66. import com.sun.org.apache.xerces.internal.xni.Augmentations;
  67. import com.sun.org.apache.xerces.internal.xni.XMLAttributes;
  68. import com.sun.org.apache.xerces.internal.xni.XMLString;
  69. import com.sun.org.apache.xerces.internal.xni.XNIException;
  70. import com.sun.org.apache.xerces.internal.util.XMLChar;
  71. import org.w3c.dom.Element;
  72. /**
  73. * A dom parser used to parse schema documents into DOM trees
  74. *
  75. * @author Sandy Gao, IBM
  76. *
  77. * @version $Id: DOMParser.java,v 1.9 2002/12/11 16:01:18 sandygao Exp $
  78. */
  79. public class DOMParser extends com.sun.org.apache.xerces.internal.parsers.DOMParser {
  80. /** Property identifier: entity manager. */
  81. protected static final String ENTITY_MANAGER =
  82. Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY;
  83. /** Property identifier: DOM document class name. */
  84. protected static final String DOCUMENT_CLASS =
  85. Constants.XERCES_PROPERTY_PREFIX + Constants.DOCUMENT_CLASS_NAME_PROPERTY;
  86. /** Feature identifier: DOM Defer node expansion. */
  87. protected static final String DEFER_EXPANSION =
  88. Constants.XERCES_FEATURE_PREFIX + Constants.DEFER_NODE_EXPANSION_FEATURE;
  89. /** Property identifier: error reporter. */
  90. public static final String ERROR_REPORTER =
  91. Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY;
  92. // the locator containing line/column information
  93. protected XMLLocator fLocator;
  94. // our own document implementation, which knows how to create Element
  95. // with line/column information
  96. public DocumentImpl fDocumentImpl;
  97. private DOMNodePool fNodePool;
  98. //
  99. // Constructors
  100. //
  101. /**
  102. * Constructs a DOM parser using the dtd/xml schema parser configuration.
  103. */
  104. public DOMParser() {
  105. // REVISIT: should we use a new configuration with scannerNS->dom API with
  106. // no dtd scanners/valitors..?
  107. //
  108. super(new NonValidatingConfiguration());
  109. try {
  110. // use our own document implementation
  111. setProperty(DOCUMENT_CLASS, "com.sun.org.apache.xerces.internal.impl.xs.dom.DocumentImpl");
  112. // don't defer DOM expansion
  113. setFeature(DEFER_EXPANSION, false);
  114. }
  115. catch (Exception e) {
  116. }
  117. fNodePool = new DOMNodePool();
  118. } // <init>()
  119. /**
  120. * Resets the node pool.
  121. */
  122. public void resetNodePool() {
  123. fNodePool.reset();
  124. }
  125. /**
  126. * The start of the document.
  127. *
  128. * @param locator The system identifier of the entity if the entity
  129. * is external, null otherwise.
  130. * @param encoding The auto-detected IANA encoding name of the entity
  131. * stream. This value will be null in those situations
  132. * where the entity encoding is not auto-detected (e.g.
  133. * internal entities or a document entity that is
  134. * parsed from a java.io.Reader).
  135. * @param namespaceContext
  136. * The namespace context in effect at the
  137. * start of this document.
  138. * This object represents the current context.
  139. * Implementors of this class are responsible
  140. * for copying the namespace bindings from the
  141. * the current context (and its parent contexts)
  142. * if that information is important.
  143. * @param augs Additional information that may include infoset augmentations
  144. *
  145. * @throws XNIException Thrown by handler to signal an error.
  146. */
  147. public void startDocument(XMLLocator locator, String encoding,
  148. NamespaceContext namespaceContext, Augmentations augs)
  149. throws XNIException {
  150. super.startDocument(locator, encoding, namespaceContext, augs);
  151. // get a handle to the document created
  152. fDocumentImpl = (DocumentImpl)super.fDocumentImpl;
  153. fDocumentImpl.fNodePool=fNodePool;
  154. fLocator = locator;
  155. } // startDocument(XMLLocator,String,Augmentations)
  156. // Where xs:appinfo or xs:documentation starts;
  157. // -1 means not in the scope of either of the two elements.
  158. private int fAnnotationDepth = -1;
  159. // The current element depth
  160. private int fDepth = -1;
  161. // Use to report the error when characters are not allowed.
  162. XMLErrorReporter fErrorReporter;
  163. // override startElement method to check whether it's one of
  164. // xs:appinfo or xs:documentation
  165. public void startElement(QName element, XMLAttributes attributes, Augmentations augs)
  166. throws XNIException {
  167. super.startElement(element, attributes, augs);
  168. fDepth++;
  169. // if it's not within either element, check whether it's one of them
  170. // if so, record the current depth, so that any element with larger
  171. // depth is allowed to have character data.
  172. if (fAnnotationDepth == -1) {
  173. if (element.uri == SchemaSymbols.URI_SCHEMAFORSCHEMA &&
  174. (element.localpart == SchemaSymbols.ELT_APPINFO ||
  175. element.localpart == SchemaSymbols.ELT_DOCUMENTATION)) {
  176. fAnnotationDepth = fDepth;
  177. }
  178. }
  179. }
  180. // override this method to check whether there are non-whitespace characters
  181. public void characters(XMLString text, Augmentations augs) throws XNIException {
  182. // when it's not within xs:appinfo or xs:documentation
  183. if (fAnnotationDepth == -1) {
  184. for (int i=text.offset; i<text.offset+text.length; i++) {
  185. // and there is a non-whitespace character
  186. if (!XMLChar.isSpace(text.ch[i])) {
  187. // only get the error reporter when reporting an error
  188. if (fErrorReporter == null) {
  189. try {
  190. fErrorReporter = (XMLErrorReporter)getProperty(ERROR_REPORTER);
  191. } catch (Exception e) {
  192. //ignore the excpetion
  193. }
  194. if (fErrorReporter.getMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN) == null) {
  195. XSMessageFormatter xmft = new XSMessageFormatter();
  196. fErrorReporter.putMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN, xmft);
  197. }
  198. }
  199. // the string we saw: starting from the first non-whitespace character.
  200. String txt = new String(text.ch, i, text.length+text.offset-i);
  201. // report an error
  202. fErrorReporter.reportError(XSMessageFormatter.SCHEMA_DOMAIN,
  203. "s4s-elt-character",
  204. new Object[]{txt},
  205. XMLErrorReporter.SEVERITY_ERROR);
  206. break;
  207. }
  208. }
  209. // don't call super.characters() when it's not within one of the 2
  210. // annotation elements: the traversers ignore them anyway. We can
  211. // save time/memory creating the text nodes.
  212. }
  213. // when it's not within either of the 2 elements, characters are allowed
  214. // and we need to call super.characters().
  215. else {
  216. super.characters(text, augs);
  217. }
  218. }
  219. // override this method to update the depth variables
  220. public void endElement(QName element, Augmentations augs) throws XNIException {
  221. super.endElement(element, augs);
  222. // when we reach the endElement of xs:appinfo or xs:documentation,
  223. // change fAnnotationDepth to -1
  224. if (fAnnotationDepth == fDepth)
  225. fAnnotationDepth = -1;
  226. fDepth--;
  227. }
  228. // override this method to store line/column information in Element created
  229. protected Element createElementNode(QName element) {
  230. // create an element containing line/column information
  231. return fDocumentImpl.createElementNS(element.uri, element.rawname,
  232. element.localpart,
  233. fLocator.getLineNumber(),
  234. fLocator.getColumnNumber());
  235. }
  236. } // class DOMParser