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.parsers;
  58. import java.util.Vector;
  59. import com.sun.org.apache.xerces.internal.dom.ASModelImpl;
  60. import com.sun.org.apache.xerces.internal.dom3.as.ASModel;
  61. import com.sun.org.apache.xerces.internal.dom3.as.DOMASBuilder;
  62. import com.sun.org.apache.xerces.internal.dom3.as.DOMASException;
  63. import com.sun.org.apache.xerces.internal.impl.Constants;
  64. import com.sun.org.apache.xerces.internal.impl.xs.SchemaGrammar;
  65. import com.sun.org.apache.xerces.internal.impl.xs.XSGrammarBucket;
  66. import com.sun.org.apache.xerces.internal.util.SymbolTable;
  67. import com.sun.org.apache.xerces.internal.util.XMLGrammarPoolImpl;
  68. import com.sun.org.apache.xerces.internal.xni.XNIException;
  69. import com.sun.org.apache.xerces.internal.xni.grammars.Grammar;
  70. import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarPool;
  71. import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
  72. import org.w3c.dom.ls.LSInput;
  73. /**
  74. * This is Abstract Schema DOM Builder class. It extends the DOMParserImpl
  75. * class. Provides support for preparsing schemas.
  76. *
  77. * @deprecated
  78. * @author Pavani Mukthipudi, Sun Microsystems Inc.
  79. * @author Neil Graham, IBM
  80. * @version $Id: DOMASBuilderImpl.java,v 1.24 2003/11/17 13:48:41 venu Exp $
  81. *
  82. */
  83. public class DOMASBuilderImpl
  84. extends DOMParserImpl implements DOMASBuilder {
  85. //
  86. // Constants
  87. //
  88. // Feature ids
  89. protected static final String SCHEMA_FULL_CHECKING =
  90. Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_FULL_CHECKING;
  91. // Property ids
  92. protected static final String ERROR_REPORTER =
  93. Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY;
  94. protected static final String SYMBOL_TABLE =
  95. Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY;
  96. protected static final String ENTITY_MANAGER =
  97. Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY;
  98. //
  99. // Data
  100. //
  101. protected XSGrammarBucket fGrammarBucket;
  102. protected ASModelImpl fAbstractSchema;
  103. //
  104. // Constructors
  105. //
  106. /**
  107. * Constructs a DOM Builder using the dtd/xml schema parser configuration.
  108. */
  109. public DOMASBuilderImpl() {
  110. super(new XMLGrammarCachingConfiguration());
  111. } // <init>
  112. /**
  113. * Constructs a DOM Builder using the specified parser configuration.
  114. * We must demand that the configuration extend XMLGrammarCachingConfiguration to make
  115. * sure all relevant methods/features are available.
  116. */
  117. public DOMASBuilderImpl(XMLGrammarCachingConfiguration config) {
  118. super(config);
  119. } // <init>(XMLParserConfiguration)
  120. /**
  121. * Constructs a DOM Builder using the specified symbol table.
  122. */
  123. public DOMASBuilderImpl(SymbolTable symbolTable) {
  124. super(new XMLGrammarCachingConfiguration(symbolTable));
  125. } // <init>(SymbolTable)
  126. /**
  127. * Constructs a DOM Builder using the specified symbol table and
  128. * grammar pool.
  129. * The grammarPool implementation should extent the default
  130. * implementation; otherwise, correct functioning of this class may
  131. * not occur.
  132. */
  133. public DOMASBuilderImpl(SymbolTable symbolTable, XMLGrammarPool grammarPool) {
  134. super(new XMLGrammarCachingConfiguration(symbolTable, grammarPool));
  135. }
  136. //
  137. // DOMASBuilder methods
  138. //
  139. /**
  140. * Associate an <code>ASModel</code> with a document instance. This
  141. * <code>ASModel</code> will be used by the "
  142. * <code>validate-if-schema</code>" and "
  143. * <code>datatype-normalization</code>" options during the load of a new
  144. * <code>Document</code>.
  145. */
  146. public ASModel getAbstractSchema() {
  147. return fAbstractSchema;
  148. }
  149. /**
  150. * Associate an <code>ASModel</code> with a document instance. This
  151. * <code>ASModel</code> will be used by the "
  152. * <code>validate-if-schema</code>" and "
  153. * <code>datatype-normalization</code>" options during the load of a new
  154. * <code>Document</code>.
  155. */
  156. public void setAbstractSchema(ASModel abstractSchema) {
  157. // since the ASModel associated with this object is an attribute
  158. // according to the DOM IDL, we must obliterate anything
  159. // that was set before, rather than adding to it.
  160. // REVISIT: so shouldn't we attempt to clear the
  161. // grammarPool before adding stuff to it? - NG
  162. fAbstractSchema = (ASModelImpl)abstractSchema;
  163. // make sure the GrammarPool is properly initialized.
  164. XMLGrammarPool grammarPool = (XMLGrammarPool)fConfiguration.getProperty(StandardParserConfiguration.XMLGRAMMAR_POOL);
  165. // if there is no grammar pool, create one
  166. // REVISIT: ASBuilder should always create one.
  167. if (grammarPool == null) {
  168. // something's not right in this situation...
  169. grammarPool = new XMLGrammarPoolImpl();
  170. fConfiguration.setProperty(StandardParserConfiguration.XMLGRAMMAR_POOL,
  171. grammarPool);
  172. }
  173. if (fAbstractSchema != null) {
  174. initGrammarPool(fAbstractSchema, grammarPool);
  175. }
  176. }
  177. /**
  178. * Parse a Abstract Schema from a location identified by an URI.
  179. *
  180. * @param uri The location of the Abstract Schema to be read.
  181. * @return The newly created <code>Abstract Schema</code>.
  182. * @exception DOMASException
  183. * Exceptions raised by <code>parseASURI()</code> originate with the
  184. * installed ErrorHandler, and thus depend on the implementation of
  185. * the <code>DOMErrorHandler</code> interfaces. The default error
  186. * handlers will raise a <code>DOMASException</code> if any form of
  187. * Abstract Schema inconsistencies or warning occurs during the parse,
  188. * but application defined errorHandlers are not required to do so.
  189. * <br> WRONG_MIME_TYPE_ERR: Raised when <code>mimeTypeCheck</code> is
  190. * <code>true</code> and the inputsource has an incorrect MIME Type.
  191. * See attribute <code>mimeTypeCheck</code>.
  192. * @exception DOMSystemException
  193. * Exceptions raised by <code>parseURI()</code> originate with the
  194. * installed ErrorHandler, and thus depend on the implementation of
  195. * the <code>DOMErrorHandler</code> interfaces. The default error
  196. * handlers will raise a DOMSystemException if any form I/O or other
  197. * system error occurs during the parse, but application defined error
  198. * handlers are not required to do so.
  199. */
  200. public ASModel parseASURI(String uri)
  201. throws DOMASException, Exception {
  202. XMLInputSource source = new XMLInputSource(null, uri, null);
  203. return parseASInputSource(source);
  204. }
  205. /**
  206. * Parse a Abstract Schema from a location identified by an
  207. * <code>LSInput</code>.
  208. *
  209. * @param is The <code>LSInput</code> from which the source
  210. * Abstract Schema is to be read.
  211. * @return The newly created <code>ASModel</code>.
  212. * @exception DOMASException
  213. * Exceptions raised by <code>parseASURI()</code> originate with the
  214. * installed ErrorHandler, and thus depend on the implementation of
  215. * the <code>DOMErrorHandler</code> interfaces. The default error
  216. * handlers will raise a <code>DOMASException</code> if any form of
  217. * Abstract Schema inconsistencies or warning occurs during the parse,
  218. * but application defined errorHandlers are not required to do so.
  219. * <br> WRONG_MIME_TYPE_ERR: Raised when <code>mimeTypeCheck</code> is
  220. * true and the inputsource has an incorrect MIME Type. See attribute
  221. * <code>mimeTypeCheck</code>.
  222. * @exception DOMSystemException
  223. * Exceptions raised by <code>parseURI()</code> originate with the
  224. * installed ErrorHandler, and thus depend on the implementation of
  225. * the <code>DOMErrorHandler</code> interfaces. The default error
  226. * handlers will raise a DOMSystemException if any form I/O or other
  227. * system error occurs during the parse, but application defined error
  228. * handlers are not required to do so.
  229. */
  230. public ASModel parseASInputSource(LSInput is)
  231. throws DOMASException, Exception {
  232. // need to wrap the LSInput with an XMLInputSource
  233. XMLInputSource xis = this.dom2xmlInputSource(is);
  234. try {
  235. return parseASInputSource(xis);
  236. }
  237. catch (XNIException e) {
  238. Exception ex = e.getException();
  239. throw ex;
  240. }
  241. }
  242. ASModel parseASInputSource(XMLInputSource is) throws Exception {
  243. if (fGrammarBucket == null) {
  244. fGrammarBucket = new XSGrammarBucket();
  245. }
  246. initGrammarBucket();
  247. // actually do the parse:
  248. // save some casting
  249. XMLGrammarCachingConfiguration gramConfig = (XMLGrammarCachingConfiguration)fConfiguration;
  250. // ensure grammarPool doesn't absorb grammars while it's parsing
  251. gramConfig.lockGrammarPool();
  252. SchemaGrammar grammar = gramConfig.parseXMLSchema(is);
  253. gramConfig.unlockGrammarPool();
  254. ASModelImpl newAsModel = null;
  255. if (grammar != null) {
  256. newAsModel = new ASModelImpl();
  257. fGrammarBucket.putGrammar (grammar, true);
  258. addGrammars(newAsModel, fGrammarBucket);
  259. }
  260. return newAsModel;
  261. }
  262. // put all the grammars we have access to in the GrammarBucket
  263. private void initGrammarBucket() {
  264. fGrammarBucket.reset();
  265. if (fAbstractSchema != null)
  266. initGrammarBucketRecurse(fAbstractSchema);
  267. }
  268. private void initGrammarBucketRecurse(ASModelImpl currModel) {
  269. if(currModel.getGrammar() != null) {
  270. fGrammarBucket.putGrammar(currModel.getGrammar());
  271. }
  272. for(int i = 0; i < currModel.getInternalASModels().size(); i++) {
  273. ASModelImpl nextModel = (ASModelImpl)(currModel.getInternalASModels().elementAt(i));
  274. initGrammarBucketRecurse(nextModel);
  275. }
  276. }
  277. private void addGrammars(ASModelImpl model, XSGrammarBucket grammarBucket) {
  278. SchemaGrammar [] grammarList = grammarBucket.getGrammars();
  279. for(int i=0; i<grammarList.length; i++) {
  280. ASModelImpl newModel = new ASModelImpl();
  281. newModel.setGrammar(grammarList[i]);
  282. model.addASModel(newModel);
  283. }
  284. } // addGrammars
  285. private void initGrammarPool(ASModelImpl currModel, XMLGrammarPool grammarPool) {
  286. // put all the grammars in fAbstractSchema into the grammar pool.
  287. // grammarPool must never be null!
  288. Grammar[] grammars = new Grammar[1];
  289. if ((grammars[0] = (Grammar)currModel.getGrammar()) != null) {
  290. grammarPool.cacheGrammars(grammars[0].getGrammarDescription().getGrammarType(), grammars);
  291. }
  292. Vector modelStore = currModel.getInternalASModels();
  293. for (int i = 0; i < modelStore.size(); i++) {
  294. initGrammarPool((ASModelImpl)modelStore.elementAt(i), grammarPool);
  295. }
  296. }
  297. } // class DOMASBuilderImpl