1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 2001-2004 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) 2001, 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;
  58. import java.util.Vector;
  59. import com.sun.org.apache.xerces.internal.impl.dv.SchemaDVFactory;
  60. import com.sun.org.apache.xerces.internal.impl.dv.ValidatedInfo;
  61. import com.sun.org.apache.xerces.internal.impl.dv.XSSimpleType;
  62. import com.sun.org.apache.xerces.internal.impl.xs.identity.IdentityConstraint;
  63. import com.sun.org.apache.xerces.internal.xs.StringList;
  64. import com.sun.org.apache.xerces.internal.xs.XSAnnotation;
  65. import com.sun.org.apache.xerces.internal.xs.XSAttributeDeclaration;
  66. import com.sun.org.apache.xerces.internal.xs.XSAttributeGroupDefinition;
  67. import com.sun.org.apache.xerces.internal.xs.XSConstants;
  68. import com.sun.org.apache.xerces.internal.xs.XSElementDeclaration;
  69. import com.sun.org.apache.xerces.internal.xs.XSModel;
  70. import com.sun.org.apache.xerces.internal.xs.XSModelGroupDefinition;
  71. import com.sun.org.apache.xerces.internal.xs.XSNamedMap;
  72. import com.sun.org.apache.xerces.internal.xs.XSNamespaceItem;
  73. import com.sun.org.apache.xerces.internal.xs.XSNotationDeclaration;
  74. import com.sun.org.apache.xerces.internal.xs.XSObjectList;
  75. import com.sun.org.apache.xerces.internal.xs.XSParticle;
  76. import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition;
  77. import com.sun.org.apache.xerces.internal.xs.XSWildcard;
  78. import com.sun.org.apache.xerces.internal.impl.xs.util.SimpleLocator;
  79. import com.sun.org.apache.xerces.internal.impl.xs.util.StringListImpl;
  80. import com.sun.org.apache.xerces.internal.impl.xs.util.XSNamedMap4Types;
  81. import com.sun.org.apache.xerces.internal.impl.xs.util.XSNamedMapImpl;
  82. import com.sun.org.apache.xerces.internal.impl.xs.util.XSObjectListImpl;
  83. import com.sun.org.apache.xerces.internal.impl.Constants;
  84. import com.sun.org.apache.xerces.internal.parsers.DOMParser;
  85. import com.sun.org.apache.xerces.internal.parsers.SAXParser;
  86. import com.sun.org.apache.xerces.internal.parsers.IntegratedParserConfiguration;
  87. import com.sun.org.apache.xerces.internal.util.SymbolHash;
  88. import com.sun.org.apache.xerces.internal.util.SymbolTable;
  89. import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription;
  90. import com.sun.org.apache.xerces.internal.xni.grammars.XSGrammar;
  91. /**
  92. * This class is to hold all schema component declaration that are declared
  93. * within one namespace.
  94. *
  95. * The Grammar class this class extends contains what little
  96. * commonality there is between XML Schema and DTD grammars. It's
  97. * useful to distinguish grammar objects from other kinds of object
  98. * when they exist in pools or caches.
  99. *
  100. * @author Sandy Gao, IBM
  101. * @author Elena Litani, IBM
  102. *
  103. * @version $Id: SchemaGrammar.java,v 1.35 2004/02/03 17:11:09 sandygao Exp $
  104. */
  105. public class SchemaGrammar implements XSGrammar, XSNamespaceItem {
  106. // the target namespace of grammar
  107. String fTargetNamespace;
  108. // global decls: map from decl name to decl object
  109. SymbolHash fGlobalAttrDecls;
  110. SymbolHash fGlobalAttrGrpDecls;
  111. SymbolHash fGlobalElemDecls;
  112. SymbolHash fGlobalGroupDecls;
  113. SymbolHash fGlobalNotationDecls;
  114. SymbolHash fGlobalIDConstraintDecls;
  115. SymbolHash fGlobalTypeDecls;
  116. // the XMLGrammarDescription member
  117. XSDDescription fGrammarDescription = null;
  118. // annotations associated with the "root" schema of this targetNamespace
  119. XSAnnotationImpl [] fAnnotations = null;
  120. // number of annotations declared
  121. int fNumAnnotations;
  122. // symbol table for constructing parsers (annotation support)
  123. private SymbolTable fSymbolTable = null;
  124. // parsers for annotation support
  125. private SAXParser fSAXParser = null;
  126. private DOMParser fDOMParser = null;
  127. //
  128. // Constructors
  129. //
  130. // needed to make BuiltinSchemaGrammar work.
  131. private SchemaGrammar() {}
  132. /**
  133. * Default constructor.
  134. *
  135. * @param targetNamespace
  136. * @param grammarDesc the XMLGrammarDescription corresponding to this objec
  137. * at the least a systemId should always be known.
  138. * @param symbolTable needed for annotation support
  139. */
  140. public SchemaGrammar(String targetNamespace, XSDDescription grammarDesc,
  141. SymbolTable symbolTable) {
  142. fTargetNamespace = targetNamespace;
  143. fGrammarDescription = grammarDesc;
  144. fSymbolTable = symbolTable;
  145. // REVISIT: do we know the numbers of the following global decls
  146. // when creating this grammar? If so, we can pass the numbers in,
  147. // and use that number to initialize the following hashtables.
  148. fGlobalAttrDecls = new SymbolHash();
  149. fGlobalAttrGrpDecls = new SymbolHash();
  150. fGlobalElemDecls = new SymbolHash();
  151. fGlobalGroupDecls = new SymbolHash();
  152. fGlobalNotationDecls = new SymbolHash();
  153. fGlobalIDConstraintDecls = new SymbolHash();
  154. // if we are parsing S4S, put built-in types in first
  155. // they might get overwritten by the types from S4S, but that's
  156. // considered what the application wants to do.
  157. if (fTargetNamespace == SchemaSymbols.URI_SCHEMAFORSCHEMA)
  158. fGlobalTypeDecls = SG_SchemaNS.fGlobalTypeDecls.makeClone();
  159. else
  160. fGlobalTypeDecls = new SymbolHash();
  161. } // <init>(String, XSDDescription)
  162. // number of built-in XSTypes we need to create for base and full
  163. // datatype set
  164. private static final int BASICSET_COUNT = 29;
  165. private static final int FULLSET_COUNT = 46;
  166. private static final int GRAMMAR_XS = 1;
  167. private static final int GRAMMAR_XSI = 2;
  168. // this class makes sure the static, built-in schema grammars
  169. // are immutable.
  170. public static class BuiltinSchemaGrammar extends SchemaGrammar {
  171. /**
  172. * Special constructor to create the grammars for the schema namespaces
  173. *
  174. * @param grammar
  175. */
  176. public BuiltinSchemaGrammar(int grammar) {
  177. SchemaDVFactory schemaFactory = SchemaDVFactory.getInstance();
  178. if (grammar == GRAMMAR_XS) {
  179. // target namespace
  180. fTargetNamespace = SchemaSymbols.URI_SCHEMAFORSCHEMA;
  181. // grammar description
  182. fGrammarDescription = new XSDDescription();
  183. fGrammarDescription.fContextType = XSDDescription.CONTEXT_PREPARSE;
  184. fGrammarDescription.setNamespace(SchemaSymbols.URI_SCHEMAFORSCHEMA);
  185. // no global decls other than types
  186. fGlobalAttrDecls = new SymbolHash(1);
  187. fGlobalAttrGrpDecls = new SymbolHash(1);
  188. fGlobalElemDecls = new SymbolHash(1);
  189. fGlobalGroupDecls = new SymbolHash(1);
  190. fGlobalNotationDecls = new SymbolHash(1);
  191. fGlobalIDConstraintDecls = new SymbolHash(1);
  192. // get all built-in types
  193. fGlobalTypeDecls = schemaFactory.getBuiltInTypes();
  194. // add anyType
  195. fGlobalTypeDecls.put(fAnyType.getName(), fAnyType);
  196. }
  197. else if (grammar == GRAMMAR_XSI) {
  198. // target namespace
  199. fTargetNamespace = SchemaSymbols.URI_XSI;
  200. // grammar description
  201. fGrammarDescription = new XSDDescription();
  202. fGrammarDescription.fContextType = XSDDescription.CONTEXT_PREPARSE;
  203. fGrammarDescription.setNamespace(SchemaSymbols.URI_XSI);
  204. // no global decls other than attributes
  205. fGlobalAttrGrpDecls = new SymbolHash(1);
  206. fGlobalElemDecls = new SymbolHash(1);
  207. fGlobalGroupDecls = new SymbolHash(1);
  208. fGlobalNotationDecls = new SymbolHash(1);
  209. fGlobalIDConstraintDecls = new SymbolHash(1);
  210. fGlobalTypeDecls = new SymbolHash(1);
  211. // 4 attributes, so initialize the size as 4*2 = 8
  212. fGlobalAttrDecls = new SymbolHash(8);
  213. String name = null;
  214. String tns = null;
  215. XSSimpleType type = null;
  216. short scope = XSConstants.SCOPE_GLOBAL;
  217. // xsi:type
  218. name = SchemaSymbols.XSI_TYPE;
  219. tns = SchemaSymbols.URI_XSI;
  220. type = schemaFactory.getBuiltInType(SchemaSymbols.ATTVAL_QNAME);
  221. fGlobalAttrDecls.put(name, new BuiltinAttrDecl(name, tns, type, scope));
  222. // xsi:nil
  223. name = SchemaSymbols.XSI_NIL;
  224. tns = SchemaSymbols.URI_XSI;
  225. type = schemaFactory.getBuiltInType(SchemaSymbols.ATTVAL_BOOLEAN);
  226. fGlobalAttrDecls.put(name, new BuiltinAttrDecl(name, tns, type, scope));
  227. XSSimpleType anyURI = schemaFactory.getBuiltInType(SchemaSymbols.ATTVAL_ANYURI);
  228. // xsi:schemaLocation
  229. name = SchemaSymbols.XSI_SCHEMALOCATION;
  230. tns = SchemaSymbols.URI_XSI;
  231. type = schemaFactory.createTypeList(null, SchemaSymbols.URI_XSI, (short)0, anyURI, null);
  232. fGlobalAttrDecls.put(name, new BuiltinAttrDecl(name, tns, type, scope));
  233. // xsi:noNamespaceSchemaLocation
  234. name = SchemaSymbols.XSI_NONAMESPACESCHEMALOCATION;
  235. tns = SchemaSymbols.URI_XSI;
  236. type = anyURI;
  237. fGlobalAttrDecls.put(name, new BuiltinAttrDecl(name, tns, type, scope));
  238. }
  239. } // <init>(int)
  240. // return the XMLGrammarDescription corresponding to this
  241. // object
  242. public XMLGrammarDescription getGrammarDescription() {
  243. return fGrammarDescription.makeClone();
  244. } // getGrammarDescription(): XMLGrammarDescription
  245. // override these methods solely so that these
  246. // objects cannot be modified once they're created.
  247. public void setImportedGrammars(Vector importedGrammars) {
  248. // ignore
  249. }
  250. public void addGlobalAttributeDecl(XSAttributeDecl decl) {
  251. // ignore
  252. }
  253. public void addGlobalAttributeGroupDecl(XSAttributeGroupDecl decl) {
  254. // ignore
  255. }
  256. public void addGlobalElementDecl(XSElementDecl decl) {
  257. // ignore
  258. }
  259. public void addGlobalGroupDecl(XSGroupDecl decl) {
  260. // ignore
  261. }
  262. public void addGlobalNotationDecl(XSNotationDecl decl) {
  263. // ignore
  264. }
  265. public void addGlobalTypeDecl(XSTypeDefinition decl) {
  266. // ignore
  267. }
  268. public void addComplexTypeDecl(XSComplexTypeDecl decl, SimpleLocator locator) {
  269. // ignore
  270. }
  271. public void addRedefinedGroupDecl(XSGroupDecl derived, XSGroupDecl base, SimpleLocator locator) {
  272. // ignore
  273. }
  274. public synchronized void addDocument(Object document, String location) {
  275. // ignore
  276. }
  277. // annotation support
  278. synchronized DOMParser getDOMParser() {
  279. return null;
  280. }
  281. synchronized SAXParser getSAXParser() {
  282. return null;
  283. }
  284. }
  285. // Grammar methods
  286. // return the XMLGrammarDescription corresponding to this
  287. // object
  288. public XMLGrammarDescription getGrammarDescription() {
  289. return fGrammarDescription;
  290. } // getGrammarDescription(): XMLGrammarDescription
  291. // DTDGrammar methods
  292. public boolean isNamespaceAware () {
  293. return true;
  294. } // isNamespaceAware():boolean
  295. Vector fImported = null;
  296. public void setImportedGrammars(Vector importedGrammars) {
  297. fImported = importedGrammars;
  298. }
  299. public Vector getImportedGrammars() {
  300. return fImported;
  301. }
  302. /**
  303. * Returns this grammar's target namespace.
  304. */
  305. public final String getTargetNamespace() {
  306. return fTargetNamespace;
  307. } // getTargetNamespace():String
  308. /**
  309. * register one global attribute
  310. */
  311. public void addGlobalAttributeDecl(XSAttributeDecl decl) {
  312. fGlobalAttrDecls.put(decl.fName, decl);
  313. }
  314. /**
  315. * register one global attribute group
  316. */
  317. public void addGlobalAttributeGroupDecl(XSAttributeGroupDecl decl) {
  318. fGlobalAttrGrpDecls.put(decl.fName, decl);
  319. }
  320. /**
  321. * register one global element
  322. */
  323. public void addGlobalElementDecl(XSElementDecl decl) {
  324. fGlobalElemDecls.put(decl.fName, decl);
  325. // if there is a substitution group affiliation, store in an array,
  326. // for further constraint checking: UPA, PD, EDC
  327. if (decl.fSubGroup != null) {
  328. if (fSubGroupCount == fSubGroups.length)
  329. fSubGroups = resize(fSubGroups, fSubGroupCount+INC_SIZE);
  330. fSubGroups[fSubGroupCount++] = decl;
  331. }
  332. }
  333. /**
  334. * register one global group
  335. */
  336. public void addGlobalGroupDecl(XSGroupDecl decl) {
  337. fGlobalGroupDecls.put(decl.fName, decl);
  338. }
  339. /**
  340. * register one global notation
  341. */
  342. public void addGlobalNotationDecl(XSNotationDecl decl) {
  343. fGlobalNotationDecls.put(decl.fName, decl);
  344. }
  345. /**
  346. * register one global type
  347. */
  348. public void addGlobalTypeDecl(XSTypeDefinition decl) {
  349. fGlobalTypeDecls.put(decl.getName(), decl);
  350. }
  351. /**
  352. * register one identity constraint
  353. */
  354. public final void addIDConstraintDecl(XSElementDecl elmDecl, IdentityConstraint decl) {
  355. elmDecl.addIDConstraint(decl);
  356. fGlobalIDConstraintDecls.put(decl.getIdentityConstraintName(), decl);
  357. }
  358. /**
  359. * get one global attribute
  360. */
  361. public final XSAttributeDecl getGlobalAttributeDecl(String declName) {
  362. return(XSAttributeDecl)fGlobalAttrDecls.get(declName);
  363. }
  364. /**
  365. * get one global attribute group
  366. */
  367. public final XSAttributeGroupDecl getGlobalAttributeGroupDecl(String declName) {
  368. return(XSAttributeGroupDecl)fGlobalAttrGrpDecls.get(declName);
  369. }
  370. /**
  371. * get one global element
  372. */
  373. public final XSElementDecl getGlobalElementDecl(String declName) {
  374. return(XSElementDecl)fGlobalElemDecls.get(declName);
  375. }
  376. /**
  377. * get one global group
  378. */
  379. public final XSGroupDecl getGlobalGroupDecl(String declName) {
  380. return(XSGroupDecl)fGlobalGroupDecls.get(declName);
  381. }
  382. /**
  383. * get one global notation
  384. */
  385. public final XSNotationDecl getGlobalNotationDecl(String declName) {
  386. return(XSNotationDecl)fGlobalNotationDecls.get(declName);
  387. }
  388. /**
  389. * get one global type
  390. */
  391. public final XSTypeDefinition getGlobalTypeDecl(String declName) {
  392. return(XSTypeDefinition)fGlobalTypeDecls.get(declName);
  393. }
  394. /**
  395. * get one identity constraint
  396. */
  397. public final IdentityConstraint getIDConstraintDecl(String declName) {
  398. return(IdentityConstraint)fGlobalIDConstraintDecls.get(declName);
  399. }
  400. /**
  401. * get one identity constraint
  402. */
  403. public final boolean hasIDConstraints() {
  404. return fGlobalIDConstraintDecls.getLength() > 0;
  405. }
  406. // array to store complex type decls
  407. private static final int INITIAL_SIZE = 16;
  408. private static final int INC_SIZE = 16;
  409. private int fCTCount = 0;
  410. private XSComplexTypeDecl[] fComplexTypeDecls = new XSComplexTypeDecl[INITIAL_SIZE];
  411. private SimpleLocator[] fCTLocators = new SimpleLocator[INITIAL_SIZE];
  412. // an array to store groups being redefined by restriction
  413. // even-numbered elements are the derived groups, odd-numbered ones their bases
  414. private static final int REDEFINED_GROUP_INIT_SIZE = 2;
  415. private int fRGCount = 0;
  416. private XSGroupDecl[] fRedefinedGroupDecls = new XSGroupDecl[REDEFINED_GROUP_INIT_SIZE];
  417. private SimpleLocator[] fRGLocators = new SimpleLocator[REDEFINED_GROUP_INIT_SIZE2];
  418. // a flag to indicate whether we have checked the 3 constraints on this
  419. // grammar.
  420. boolean fFullChecked = false;
  421. /**
  422. * add one complex type decl: for later constraint checking
  423. */
  424. public void addComplexTypeDecl(XSComplexTypeDecl decl, SimpleLocator locator) {
  425. if (fCTCount == fComplexTypeDecls.length) {
  426. fComplexTypeDecls = resize(fComplexTypeDecls, fCTCount+INC_SIZE);
  427. fCTLocators = resize(fCTLocators, fCTCount+INC_SIZE);
  428. }
  429. fCTLocators[fCTCount] = locator;
  430. fComplexTypeDecls[fCTCount++] = decl;
  431. }
  432. /**
  433. * add a group redefined by restriction: for later constraint checking
  434. */
  435. public void addRedefinedGroupDecl(XSGroupDecl derived, XSGroupDecl base, SimpleLocator locator) {
  436. if (fRGCount == fRedefinedGroupDecls.length) {
  437. // double array size each time.
  438. fRedefinedGroupDecls = resize(fRedefinedGroupDecls, fRGCount << 1);
  439. fRGLocators = resize(fRGLocators, fRGCount);
  440. }
  441. fRGLocators[fRGCount2] = locator;
  442. fRedefinedGroupDecls[fRGCount++] = derived;
  443. fRedefinedGroupDecls[fRGCount++] = base;
  444. }
  445. /**
  446. * get all complex type decls: for later constraint checking
  447. */
  448. final XSComplexTypeDecl[] getUncheckedComplexTypeDecls() {
  449. if (fCTCount < fComplexTypeDecls.length) {
  450. fComplexTypeDecls = resize(fComplexTypeDecls, fCTCount);
  451. fCTLocators = resize(fCTLocators, fCTCount);
  452. }
  453. return fComplexTypeDecls;
  454. }
  455. /**
  456. * get the error locator of all complex type decls
  457. */
  458. final SimpleLocator[] getUncheckedCTLocators() {
  459. if (fCTCount < fCTLocators.length) {
  460. fComplexTypeDecls = resize(fComplexTypeDecls, fCTCount);
  461. fCTLocators = resize(fCTLocators, fCTCount);
  462. }
  463. return fCTLocators;
  464. }
  465. /**
  466. * get all redefined groups: for later constraint checking
  467. */
  468. final XSGroupDecl[] getRedefinedGroupDecls() {
  469. if (fRGCount < fRedefinedGroupDecls.length) {
  470. fRedefinedGroupDecls = resize(fRedefinedGroupDecls, fRGCount);
  471. fRGLocators = resize(fRGLocators, fRGCount2);
  472. }
  473. return fRedefinedGroupDecls;
  474. }
  475. /**
  476. * get the error locator of all redefined groups
  477. */
  478. final SimpleLocator[] getRGLocators() {
  479. if (fRGCount < fRedefinedGroupDecls.length) {
  480. fRedefinedGroupDecls = resize(fRedefinedGroupDecls, fRGCount);
  481. fRGLocators = resize(fRGLocators, fRGCount2);
  482. }
  483. return fRGLocators;
  484. }
  485. /**
  486. * after the first-round checking, some types don't need to be checked
  487. * against UPA again. here we trim the array to the proper size.
  488. */
  489. final void setUncheckedTypeNum(int newSize) {
  490. fCTCount = newSize;
  491. fComplexTypeDecls = resize(fComplexTypeDecls, fCTCount);
  492. fCTLocators = resize(fCTLocators, fCTCount);
  493. }
  494. // used to store all substitution group information declared in
  495. // this namespace
  496. private int fSubGroupCount = 0;
  497. private XSElementDecl[] fSubGroups = new XSElementDecl[INITIAL_SIZE];
  498. /**
  499. * get all substitution group information: for the 3 constraint checking
  500. */
  501. final XSElementDecl[] getSubstitutionGroups() {
  502. if (fSubGroupCount < fSubGroups.length)
  503. fSubGroups = resize(fSubGroups, fSubGroupCount);
  504. return fSubGroups;
  505. }
  506. // anyType and anySimpleType: because there are so many places where
  507. // we need direct access to these two types
  508. public final static XSComplexTypeDecl fAnyType = new XSAnyType();
  509. private static class XSAnyType extends XSComplexTypeDecl {
  510. public XSAnyType () {
  511. fName = SchemaSymbols.ATTVAL_ANYTYPE;
  512. super.fTargetNamespace = SchemaSymbols.URI_SCHEMAFORSCHEMA;
  513. fBaseType = this;
  514. fDerivedBy = XSConstants.DERIVATION_RESTRICTION;
  515. fContentType = XSComplexTypeDecl.CONTENTTYPE_MIXED;
  516. fParticle = null;
  517. fAttrGrp = null;
  518. }
  519. // overridden methods
  520. public void setValues(String name, String targetNamespace,
  521. XSTypeDefinition baseType, short derivedBy, short schemaFinal,
  522. short block, short contentType,
  523. boolean isAbstract, XSAttributeGroupDecl attrGrp,
  524. XSSimpleType simpleType, XSParticleDecl particle) {
  525. // don't allow this.
  526. }
  527. public void setName(String name){
  528. // don't allow this.
  529. }
  530. public void setIsAbstractType() {
  531. // null implementation
  532. }
  533. public void setContainsTypeID() {
  534. // null implementation
  535. }
  536. public void setIsAnonymous() {
  537. // null implementation
  538. }
  539. public void reset() {
  540. // null implementation
  541. }
  542. public XSObjectList getAttributeUses() {
  543. return new XSObjectListImpl(null, 0);
  544. }
  545. public XSAttributeGroupDecl getAttrGrp() {
  546. XSWildcardDecl wildcard = new XSWildcardDecl();
  547. wildcard.fProcessContents = XSWildcardDecl.PC_LAX;
  548. XSAttributeGroupDecl attrGrp = new XSAttributeGroupDecl();
  549. attrGrp.fAttributeWC = wildcard;
  550. return attrGrp;
  551. }
  552. public XSWildcard getAttributeWildcard() {
  553. XSWildcardDecl wildcard = new XSWildcardDecl();
  554. wildcard.fProcessContents = XSWildcardDecl.PC_LAX;
  555. return wildcard;
  556. }
  557. public XSParticle getParticle() {
  558. // the wildcard used in anyType (content and attribute)
  559. // the spec will change strict to skip for anyType
  560. XSWildcardDecl wildcard = new XSWildcardDecl();
  561. wildcard.fProcessContents = XSWildcardDecl.PC_LAX;
  562. // the particle for the content wildcard
  563. XSParticleDecl particleW = new XSParticleDecl();
  564. particleW.fMinOccurs = 0;
  565. particleW.fMaxOccurs = SchemaSymbols.OCCURRENCE_UNBOUNDED;
  566. particleW.fType = XSParticleDecl.PARTICLE_WILDCARD;
  567. particleW.fValue = wildcard;
  568. // the model group of a sequence of the above particle
  569. XSModelGroupImpl group = new XSModelGroupImpl();
  570. group.fCompositor = XSModelGroupImpl.MODELGROUP_SEQUENCE;
  571. group.fParticleCount = 1;
  572. group.fParticles = new XSParticleDecl[1];
  573. group.fParticles[0] = particleW;
  574. // the content of anyType: particle of the above model group
  575. XSParticleDecl particleG = new XSParticleDecl();
  576. particleG.fType = XSParticleDecl.PARTICLE_MODELGROUP;
  577. particleG.fValue = group;
  578. return particleG;
  579. }
  580. public XSObjectList getAnnotations() {
  581. return null;
  582. }
  583. }
  584. private static class BuiltinAttrDecl extends XSAttributeDecl {
  585. public BuiltinAttrDecl(String name, String tns,
  586. XSSimpleType type, short scope) {
  587. fName = name;
  588. super.fTargetNamespace = tns;
  589. fType = type;
  590. fScope = scope;
  591. }
  592. public void setValues(String name, String targetNamespace,
  593. XSSimpleType simpleType, short constraintType, short scope,
  594. ValidatedInfo valInfo, XSComplexTypeDecl enclosingCT) {
  595. // ignore this call.
  596. }
  597. public void reset () {
  598. // also ignore this call.
  599. }
  600. public XSAnnotation getAnnotation() {
  601. return null;
  602. }
  603. } // class BuiltinAttrDecl
  604. // the grammars to hold components of the schema namespace
  605. public final static BuiltinSchemaGrammar SG_SchemaNS = new BuiltinSchemaGrammar(GRAMMAR_XS);
  606. public final static XSSimpleType fAnySimpleType = (XSSimpleType)SG_SchemaNS.getGlobalTypeDecl(SchemaSymbols.ATTVAL_ANYSIMPLETYPE);
  607. // the grammars to hold components of the schema-instance namespace
  608. public final static BuiltinSchemaGrammar SG_XSI = new BuiltinSchemaGrammar(GRAMMAR_XSI);
  609. static final XSComplexTypeDecl[] resize(XSComplexTypeDecl[] oldArray, int newSize) {
  610. XSComplexTypeDecl[] newArray = new XSComplexTypeDecl[newSize];
  611. System.arraycopy(oldArray, 0, newArray, 0, Math.min(oldArray.length, newSize));
  612. return newArray;
  613. }
  614. static final XSGroupDecl[] resize(XSGroupDecl[] oldArray, int newSize) {
  615. XSGroupDecl[] newArray = new XSGroupDecl[newSize];
  616. System.arraycopy(oldArray, 0, newArray, 0, Math.min(oldArray.length, newSize));
  617. return newArray;
  618. }
  619. static final XSElementDecl[] resize(XSElementDecl[] oldArray, int newSize) {
  620. XSElementDecl[] newArray = new XSElementDecl[newSize];
  621. System.arraycopy(oldArray, 0, newArray, 0, Math.min(oldArray.length, newSize));
  622. return newArray;
  623. }
  624. static final SimpleLocator[] resize(SimpleLocator[] oldArray, int newSize) {
  625. SimpleLocator[] newArray = new SimpleLocator[newSize];
  626. System.arraycopy(oldArray, 0, newArray, 0, Math.min(oldArray.length, newSize));
  627. return newArray;
  628. }
  629. // XSNamespaceItem methods
  630. // the max index / the max value of XSObject type
  631. private static final short MAX_COMP_IDX = XSTypeDefinition.SIMPLE_TYPE;
  632. private static final boolean[] GLOBAL_COMP = {false, // null
  633. true, // attribute
  634. true, // element
  635. true, // type
  636. false, // attribute use
  637. true, // attribute group
  638. true, // group
  639. false, // model group
  640. false, // particle
  641. false, // wildcard
  642. false, // idc
  643. true, // notation
  644. false, // annotation
  645. true, // complex type
  646. true // simple type
  647. };
  648. // store a certain kind of components from all namespaces
  649. private XSNamedMap[] fComponents = null;
  650. // store the documents and their locations contributing to this namespace
  651. // REVISIT: use StringList and XSObjectList for there fields.
  652. private Vector fDocuments = null;
  653. private Vector fLocations = null;
  654. public synchronized void addDocument(Object document, String location) {
  655. if (fDocuments == null) {
  656. fDocuments = new Vector();
  657. fLocations = new Vector();
  658. }
  659. fDocuments.addElement(document);
  660. fLocations.addElement(location);
  661. }
  662. /**
  663. * [schema namespace]
  664. * @see <a href="http://www.w3.org/TR/xmlschema-1/#nsi-schema_namespace">[schema namespace]</a>
  665. * @return The target namespace of this item.
  666. */
  667. public String getSchemaNamespace() {
  668. return fTargetNamespace;
  669. }
  670. // annotation support
  671. synchronized DOMParser getDOMParser() {
  672. if (fDOMParser != null) return fDOMParser;
  673. // REVISIT: when schema handles XML 1.1, will need to
  674. // revisit this (and the practice of not prepending an XML decl to the annotation string
  675. IntegratedParserConfiguration config = new IntegratedParserConfiguration(fSymbolTable);
  676. // note that this should never produce errors or require
  677. // entity resolution, so just a barebones configuration with
  678. // a couple of feature set will do fine
  679. config.setFeature(Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE, true);
  680. config.setFeature(Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE, false);
  681. fDOMParser = new DOMParser(config);
  682. return fDOMParser;
  683. }
  684. synchronized SAXParser getSAXParser() {
  685. if (fSAXParser != null) return fSAXParser;
  686. // REVISIT: when schema handles XML 1.1, will need to
  687. // revisit this (and the practice of not prepending an XML decl to the annotation string
  688. IntegratedParserConfiguration config = new IntegratedParserConfiguration(fSymbolTable);
  689. // note that this should never produce errors or require
  690. // entity resolution, so just a barebones configuration with
  691. // a couple of feature set will do fine
  692. config.setFeature(Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE, true);
  693. config.setFeature(Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE, false);
  694. fSAXParser = new SAXParser(config);
  695. return fSAXParser;
  696. }
  697. /**
  698. * Returns a list of top-level components, i.e. element declarations,
  699. * attribute declarations, etc.<p>
  700. * Note that <code>XSTypeDefinition#SIMPLE_TYPE</code> and
  701. * <code>XSTypeDefinition#COMPLEX_TYPE</code> can also be used as the
  702. * <code>objectType</code> to retrieve only complex types or simple types,
  703. * instead of all types.
  704. * @param objectType The type of the declaration, i.e.
  705. * ELEMENT_DECLARATION, ATTRIBUTE_DECLARATION, etc.
  706. * @return A list of top-level definition of the specified type in
  707. * <code>objectType</code> or <code>null</code>.
  708. */
  709. public synchronized XSNamedMap getComponents(short objectType) {
  710. if (objectType <= 0 || objectType > MAX_COMP_IDX ||
  711. !GLOBAL_COMP[objectType]) {
  712. return null;
  713. }
  714. if (fComponents == null)
  715. fComponents = new XSNamedMap[MAX_COMP_IDX+1];
  716. // get the hashtable for this type of components
  717. if (fComponents[objectType] == null) {
  718. SymbolHash table = null;
  719. switch (objectType) {
  720. case XSConstants.TYPE_DEFINITION:
  721. case XSTypeDefinition.COMPLEX_TYPE:
  722. case XSTypeDefinition.SIMPLE_TYPE:
  723. table = fGlobalTypeDecls;
  724. break;
  725. case XSConstants.ATTRIBUTE_DECLARATION:
  726. table = fGlobalAttrDecls;
  727. break;
  728. case XSConstants.ELEMENT_DECLARATION:
  729. table = fGlobalElemDecls;
  730. break;
  731. case XSConstants.ATTRIBUTE_GROUP:
  732. table = fGlobalAttrGrpDecls;
  733. break;
  734. case XSConstants.MODEL_GROUP_DEFINITION:
  735. table = fGlobalGroupDecls;
  736. break;
  737. case XSConstants.NOTATION_DECLARATION:
  738. table = fGlobalNotationDecls;
  739. break;
  740. }
  741. // for complex/simple types, create a special implementation,
  742. // which take specific types out of the hash table
  743. if (objectType == XSTypeDefinition.COMPLEX_TYPE ||
  744. objectType == XSTypeDefinition.SIMPLE_TYPE) {
  745. fComponents[objectType] = new XSNamedMap4Types(fTargetNamespace, table, objectType);
  746. }
  747. else {
  748. fComponents[objectType] = new XSNamedMapImpl(fTargetNamespace, table);
  749. }
  750. }
  751. return fComponents[objectType];
  752. }
  753. /**
  754. * Convenience method. Returns a top-level simple or complex type
  755. * definition.
  756. * @param name The name of the definition.
  757. * @return An <code>XSTypeDefinition</code> or null if such definition
  758. * does not exist.
  759. */
  760. public XSTypeDefinition getTypeDefinition(String name) {
  761. return getGlobalTypeDecl(name);
  762. }
  763. /**
  764. * Convenience method. Returns a top-level attribute declaration.
  765. * @param name The name of the declaration.
  766. * @return A top-level attribute declaration or null if such declaration
  767. * does not exist.
  768. */
  769. public XSAttributeDeclaration getAttributeDeclaration(String name) {
  770. return getGlobalAttributeDecl(name);
  771. }
  772. /**
  773. * Convenience method. Returns a top-level element declaration.
  774. * @param name The name of the declaration.
  775. * @return A top-level element declaration or null if such declaration
  776. * does not exist.
  777. */
  778. public XSElementDeclaration getElementDeclaration(String name) {
  779. return getGlobalElementDecl(name);
  780. }
  781. /**
  782. * Convenience method. Returns a top-level attribute group definition.
  783. * @param name The name of the definition.
  784. * @return A top-level attribute group definition or null if such
  785. * definition does not exist.
  786. */
  787. public XSAttributeGroupDefinition getAttributeGroup(String name) {
  788. return getGlobalAttributeGroupDecl(name);
  789. }
  790. /**
  791. * Convenience method. Returns a top-level model group definition.
  792. *
  793. * @param name The name of the definition.
  794. * @return A top-level model group definition definition or null if such
  795. * definition does not exist.
  796. */
  797. public XSModelGroupDefinition getModelGroupDefinition(String name) {
  798. return getGlobalGroupDecl(name);
  799. }
  800. /**
  801. * Convenience method. Returns a top-level notation declaration.
  802. *
  803. * @param name The name of the declaration.
  804. * @return A top-level notation declaration or null if such declaration
  805. * does not exist.
  806. */
  807. public XSNotationDeclaration getNotationDeclaration(String name) {
  808. return getGlobalNotationDecl(name);
  809. }
  810. /**
  811. * [document location]
  812. * @see <a href="http://www.w3.org/TR/xmlschema-1/#sd-document_location">[document location]</a>
  813. * @return a list of document information item
  814. */
  815. public StringList getDocumentLocations() {
  816. return new StringListImpl(fLocations);
  817. }
  818. /**
  819. * Return an <code>XSModel</code> that represents components in this schema
  820. * grammar.
  821. *
  822. * @return an <code>XSModel</code> representing this schema grammar
  823. */
  824. public XSModel toXSModel() {
  825. return new XSModelImpl(new SchemaGrammar[]{this});
  826. }
  827. public XSModel toXSModel(XSGrammar[] grammars) {
  828. if (grammars == null || grammars.length == 0)
  829. return toXSModel();
  830. int len = grammars.length;
  831. boolean hasSelf = false;
  832. for (int i = 0; i < len; i++) {
  833. if (grammars[i] == this) {
  834. hasSelf = true;
  835. break;
  836. }
  837. }
  838. SchemaGrammar[] gs = new SchemaGrammar[hasSelf ? len : len+1];
  839. for (int i = 0; i < len; i++)
  840. gs[i] = (SchemaGrammar)grammars[i];
  841. if (!hasSelf)
  842. gs[len] = this;
  843. return new XSModelImpl(gs);
  844. }
  845. /**
  846. * @see com.sun.org.apache.xerces.internal.xs.XSNamespaceItem#getAnnotations()
  847. */
  848. public XSObjectList getAnnotations() {
  849. return new XSObjectListImpl(fAnnotations, fNumAnnotations);
  850. }
  851. public void addAnnotation(XSAnnotationImpl annotation) {
  852. if(annotation == null)
  853. return;
  854. if(fAnnotations == null) {
  855. fAnnotations = new XSAnnotationImpl[2];
  856. } else if(fNumAnnotations == fAnnotations.length) {
  857. XSAnnotationImpl[] newArray = new XSAnnotationImpl[fNumAnnotations << 1];
  858. System.arraycopy(fAnnotations, 0, newArray, 0, fNumAnnotations);
  859. fAnnotations = newArray;
  860. }
  861. fAnnotations[fNumAnnotations++] = annotation;
  862. }
  863. } // class SchemaGrammar