KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > apache > xerces > validators > dtd > DTDGrammar


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2000 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
58 package org.enhydra.apache.xerces.validators.dtd;
59
60 import org.enhydra.apache.xerces.dom.DocumentImpl;
61 import org.enhydra.apache.xerces.framework.XMLContentSpec;
62 import org.enhydra.apache.xerces.framework.XMLDTDScanner;
63 import org.enhydra.apache.xerces.utils.QName;
64 import org.enhydra.apache.xerces.utils.StringPool;
65 import org.enhydra.apache.xerces.validators.common.Grammar;
66 import org.enhydra.apache.xerces.validators.common.XMLAttributeDecl;
67 import org.enhydra.apache.xerces.validators.common.XMLElementDecl;
68 import org.w3c.dom.Document JavaDoc;
69 import org.w3c.dom.Element JavaDoc;
70 import org.w3c.dom.ProcessingInstruction JavaDoc;
71
72 /**
73  * A DTD grammar. This class is an EventHandler to receive callbacks from
74  * the XMLDTDScanner. When the callbacks are received, the grammar structures
75  * are directly populated from the callback information.
76  * <p>
77  * In addition to being a recipient of scanner callbacks, the DTD grammar
78  * class can act as a pass-through filter for the DTD events. This is useful
79  * for parsers that must expose the DTD information to the application. (e.g.
80  * SAX2 DeclHandler callbacks.)
81  *
82  * @author Andy Clark
83  * @version $Id: DTDGrammar.java,v 1.2 2005/01/26 08:28:44 jkjome Exp $
84  */

85 public class DTDGrammar
86     extends Grammar
87     implements XMLDTDScanner.EventHandler {
88
89     // REVISIT: The grammar access currently implemented in this grammar
90
// instance is a draft implementation and should be revisited
91
// in order to design a proper DTD for the this grammar
92
// representation. -Ac
93

94     //
95
// Constants
96
//
97

98     /** Chunk shift. */
99     private static final int CHUNK_SHIFT = 8; // 2^8 = 256
100

101     /** Chunk size. */
102     private static final int CHUNK_SIZE = (1 << CHUNK_SHIFT);
103
104     /** Chunk mask. */
105     private static final int CHUNK_MASK = CHUNK_SIZE - 1;
106
107     /** Initial chunk count. */
108     private static final int INITIAL_CHUNK_COUNT = (1 << (10 - CHUNK_SHIFT)); // 2^10 = 1k
109

110     //
111
// Data
112
//
113

114     // string pool
115

116     /** String pool. */
117     private StringPool fStringPool;
118
119     // "compiled" information structures
120

121     /** Element declaration. */
122     private XMLElementDecl fElementDecl = new XMLElementDecl();
123
124     /** Attribute declaration. */
125     private XMLAttributeDecl fAttributeDecl = new XMLAttributeDecl();
126
127     /** Content spec node. */
128     private XMLContentSpec fContentSpec = new XMLContentSpec();
129
130     // grammar document
131

132     /** Grammar document. */
133     private Document JavaDoc fGrammarDocument;
134
135     /** Root element. */
136     private Element JavaDoc fRootElement;
137
138     private QName fRootElementQName = new QName();
139
140     /** Current element. */
141     private Element JavaDoc fCurrentElement;
142
143     // pass-through
144

145     /** flag if the elementDecl is External. */
146     private int fElementDeclIsExternal[][] = new int[INITIAL_CHUNK_COUNT][];
147     /** Mapping for element declarations. */
148     private int fElementDeclMap[][] = new int[INITIAL_CHUNK_COUNT][];
149
150     /** flag if the AttributeDecl is External. */
151     private int fAttributeDeclIsExternal[][] = new int[INITIAL_CHUNK_COUNT][];
152     /** Mapping for attribute declarations. */
153     private int fAttributeDeclMap[][] = new int[INITIAL_CHUNK_COUNT][];
154
155     /** Mapping for content spec nodes. */
156     private int fContentSpecMap[][] = new int[INITIAL_CHUNK_COUNT][];
157
158     // temp vars
159

160     private QName fQName = new QName();
161
162     //
163
// Constructors
164
//
165

166     /** Default constructor. */
167     public DTDGrammar(StringPool stringPool) {
168         reset(stringPool);
169     }
170
171     //
172
// Public methods
173
//
174

175     /** Resets the DTD grammar. */
176     public void reset(StringPool stringPool) {
177         fStringPool = stringPool;
178     }
179
180     //
181
// XMLDTDScanner.EventHandler methods
182
//
183

184     /** Start of DTD. */
185     public void callStartDTD() throws Exception JavaDoc {
186         
187         // setup grammar document
188
setGrammarDocument(null);
189         fGrammarDocument = new DocumentImpl();
190         fRootElement = fGrammarDocument.createElement("dtd");
191         fCurrentElement = fRootElement;
192
193     } // callStartDTD()
194

195     /** End of DTD. */
196     public void callEndDTD() throws Exception JavaDoc {
197
198         // set grammar document
199
setGrammarDocument(fGrammarDocument);
200
201     } // callEndDTD()
202

203     /**
204      * Signal the Text declaration of an external entity.
205      *
206      * @param version the handle in the string pool for the version number
207      * @param encoding the handle in the string pool for the encoding
208      * @exception java.lang.Exception
209      */

210     public void callTextDecl(int version, int encoding) throws Exception JavaDoc {
211
212         // create text decl
213
Element JavaDoc textDecl = fGrammarDocument.createElement("textDecl");
214         textDecl.setAttribute("version", fStringPool.toString(version));
215         textDecl.setAttribute("encoding", fStringPool.toString(encoding));
216         fCurrentElement.appendChild(textDecl);
217
218     } // callTextDecl(int,int)
219

220     /**
221      * Called when the doctype decl is scanned
222      *
223      * @param rootElementType handle of the rootElement
224      * @param publicId StringPool handle of the public id
225      * @param systemId StringPool handle of the system id
226      * @exception java.lang.Exception
227      */

228     public void doctypeDecl(QName rootElement, int publicId, int systemId)
229         throws Exception JavaDoc {
230
231         // create doctype decl
232
Element JavaDoc doctypeDecl = fGrammarDocument.createElement("doctypeDecl");
233         doctypeDecl.setAttribute("name", fStringPool.toString(rootElement.rawname));
234         if (rootElement.uri != StringPool.EMPTY_STRING) {
235             doctypeDecl.setAttribute("xmlns:"+fStringPool.toString(rootElement.prefix),
236                                      fStringPool.toString(rootElement.uri));
237         }
238         doctypeDecl.setAttribute("publicId", fStringPool.toString(publicId));
239         doctypeDecl.setAttribute("systemId", fStringPool.toString(systemId));
240         fCurrentElement.appendChild(doctypeDecl);
241
242         //fRootElementQName.setValues(-1, rootElement.rawname, -1, -1);
243
fRootElementQName.setValues(rootElement);
244
245     } // doctypeDecl(QName,int,int);
246

247     /**
248      * Called when the DTDScanner starts reading from the external subset
249      *
250      * @param publicId StringPool handle of the public id
251      * @param systemId StringPool handle of the system id
252      * @exception java.lang.Exception
253      */

254     public void startReadingFromExternalSubset(int publicId, int systemId)
255         throws Exception JavaDoc {
256
257         // create external subset
258
Element JavaDoc externalSubset = fGrammarDocument.createElement("external");
259         externalSubset.setAttribute("publicId", fStringPool.toString(publicId));
260         externalSubset.setAttribute("systemId", fStringPool.toString(systemId));
261         fCurrentElement.appendChild(externalSubset);
262         fCurrentElement = externalSubset;
263
264     } // startReadingFromExternalSubset(int,int)
265

266     /**
267      * Called when the DTDScanner stop reading from the external subset
268      *
269      * @exception java.lang.Exception
270      */

271     public void stopReadingFromExternalSubset() throws Exception JavaDoc {
272
273         // get out of external subset
274
fCurrentElement = (Element JavaDoc)fCurrentElement.getParentNode();
275
276     } // stopReadingFromExternalSubset()
277

278     /**
279      * Add an element declaration (forward reference)
280      *
281      * @param handle to the name of the element being declared
282      * @return handle to the element whose declaration was added
283      * @exception java.lang.Exception
284      */

285     public int addElementDecl(QName elementDecl) throws Exception JavaDoc {
286
287         // create element decl element
288
Element JavaDoc elementDeclElement = fGrammarDocument.createElement("elementDecl");
289         elementDeclElement.setAttribute("name", fStringPool.toString(elementDecl.localpart));
290         if (elementDecl.uri != StringPool.EMPTY_STRING) {
291             elementDeclElement.setAttribute("xmlns:"+fStringPool.toString(elementDecl.prefix),
292                                             fStringPool.toString(elementDecl.uri));
293         }
294         fCurrentElement.appendChild(elementDeclElement);
295
296         // create element decl
297
int elementDeclIndex = createElementDecl();
298
299         // set element decl values
300
fElementDecl.clear();
301         fElementDecl.name.setValues(elementDecl);
302         setElementDecl(elementDeclIndex, fElementDecl);
303
304         // return index
305
return elementDeclIndex;
306
307     } // addElementDecl(QName):int
308

309     /**
310      * Add an element declaration
311      *
312      * @param handle to the name of the element being declared
313      * @param contentSpecType handle to the type name of the content spec
314      * @param ContentSpec handle to the content spec node for the contentSpecType
315      * @return handle to the element declaration that was added
316      * @exception java.lang.Exception
317      */

318     public int addElementDecl(QName elementDecl,
319                               int contentSpecType,
320                               int contentSpec,
321                               boolean isExternal) throws Exception JavaDoc {
322
323         // create element decl element
324
Element JavaDoc elementDeclElement = fGrammarDocument.createElement("elementDecl");
325         elementDeclElement.setAttribute("name", fStringPool.toString(elementDecl.localpart));
326         if (elementDecl.uri != StringPool.EMPTY_STRING) {
327             elementDeclElement.setAttribute("xmlns:"+fStringPool.toString(elementDecl.prefix),
328                                             fStringPool.toString(elementDecl.uri));
329         }
330         elementDeclElement.setAttribute("type", fStringPool.toString(contentSpecType));
331         // REVISIT: Traverse content spec structure, building content model
332
// description to put into grammar document.
333
fCurrentElement.appendChild(elementDeclElement);
334
335         // create element decl
336
int elementDeclIndex = createElementDecl();
337
338         // set element decl values
339
fElementDecl.clear();
340         fElementDecl.name.setValues(elementDecl);
341         fElementDecl.type = contentSpecType;
342         fElementDecl.contentSpecIndex = contentSpec;
343         setElementDecl(elementDeclIndex, fElementDecl);
344         
345         int chunk = elementDeclIndex >> CHUNK_SHIFT;
346         int index = elementDeclIndex & CHUNK_MASK;
347         ensureElementDeclCapacity(chunk);
348         fElementDeclIsExternal[chunk][index] = isExternal? 1 : 0;
349
350         // return index
351
return elementDeclIndex;
352
353     } // addElementDecl(QName,int,int):int
354

355     protected void putElementNameMapping(QName name, int scope,
356                                          int elementDeclIndex) {
357         fQName.uri = StringPool.EMPTY_STRING;
358         fQName.localpart = name.rawname;
359         super.putElementNameMapping(fQName, scope, elementDeclIndex);
360     }
361
362     /***
363     public int getElementDeclIndex(int localpartIndex, int scopeIndex) {
364         //System.out.println("getElementDeclIndex: "+localpartIndex+", "+scopeIndex);
365         return super.getElementDeclIndex(localpartIndex, scopeIndex);
366     }
367
368     public int getElementDeclIndex(int uriIndex, int localpartIndex, int scopeIndex) {
369         //System.out.println("!!! getElementDeclIndex: "+uriIndex+", "+localpartIndex+", "+scopeIndex);
370         return super.getElementDeclIndex(localpartIndex, -1);
371     }
372     /***/

373     
374     public int getElementDeclIndex(QName element, int scopeIndex) {
375         //System.out.println("getElementDeclIndex: "+element+", "+scopeIndex);
376
return super.getElementDeclIndex(element.rawname, -1);
377     }
378
379     public void setElementDeclDTD(int elementDeclIndex, XMLElementDecl elementDecl) {
380         super.setElementDecl(elementDeclIndex, elementDecl);
381     }
382
383     private XMLContentSpec fTempContentSpec = new XMLContentSpec();
384
385     /***
386     public void setContentSpecLeaf(int contentSpecIndex, QName elementName) {
387         fTempContentSpec.setValues(XMLContentSpec.CONTENTSPECNODE_LEAF, elementName.rawname, -1);
388         super.setContentSpec(contentSpecIndex, fTempContentSpec);
389     }
390     /***/

391
392     public void setElementDeclIsExternal(int elementDeclIndex, boolean isExternal) {
393         int chunk = elementDeclIndex >> CHUNK_SHIFT;
394         int index = elementDeclIndex & CHUNK_MASK;
395         ensureElementDeclCapacity(chunk);
396         fElementDeclIsExternal[chunk][index] = isExternal? 1 : 0;
397     }
398
399     // getters for isExternals
400
public boolean getElementDeclIsExternal(int elementDeclIndex) {
401         if (elementDeclIndex < 0) {
402             return false;
403         }
404         int chunk = elementDeclIndex >> CHUNK_SHIFT;
405         int index = elementDeclIndex & CHUNK_MASK;
406         return (fElementDeclIsExternal[chunk][index] != 0);
407     }
408
409     public boolean getAttributeDeclIsExternal(int attributeDeclIndex) {
410         if (attributeDeclIndex < 0) {
411             return false;
412         }
413         int chunk = attributeDeclIndex >> CHUNK_SHIFT;
414         int index = attributeDeclIndex & CHUNK_MASK;
415         return (fAttributeDeclIsExternal[chunk][index] != 0);
416     }
417
418     public boolean getRootElementQName(QName root) {
419         if (fRootElementQName.rawname == -1) {
420             return false;
421         }
422         root.setValues(fRootElementQName);
423         return true;
424     }
425
426     /**
427      * Add an attribute definition
428      *
429      * @param handle to the element whose attribute is being declared
430      * @param attName StringPool handle to the attribute name being declared
431      * @param attType type of the attribute
432      * @param enumeration StringPool handle of the attribute's enumeration list (if any)
433      * @param attDefaultType an integer value denoting the DefaultDecl value
434      * @param attDefaultValue StringPool handle of this attribute's default value
435      * @return handle to the attribute definition
436      * @exception java.lang.Exception
437      */

438     public int addAttDef(QName elementDecl, QName attributeDecl,
439                          int attType, boolean attList, int enumeration,
440                          int attDefaultType, int attDefaultValue, boolean isExternal)
441         throws Exception JavaDoc {
442         /****
443         System.out.println("---add attr--- "+attributeDecl.localpart
444                   +","+attType
445                   +","+attDefaultType
446                   +","+isExternal);
447          /****/

448
449         // create attribute decl element
450
Element JavaDoc attributeDeclElement = fGrammarDocument.createElement("attributeDecl");
451         attributeDeclElement.setAttribute("element", fStringPool.toString(elementDecl.localpart));
452         attributeDeclElement.setAttribute("name", fStringPool.toString(attributeDecl.localpart));
453         if (attributeDecl.uri != StringPool.EMPTY_STRING) {
454             attributeDeclElement.setAttribute("xmlns:"+fStringPool.toString(attributeDecl.prefix),
455                                               fStringPool.toString(attributeDecl.uri));
456         }
457         attributeDeclElement.setAttribute("type", fStringPool.toString(attType));
458         // REVISIT: Add enumeration information to grammar document.
459
// REVISIT: Do the default type, value better.
460
attributeDeclElement.setAttribute("defaultType", fStringPool.toString(attDefaultType));
461         attributeDeclElement.setAttribute("defaultValue", fStringPool.toString(attDefaultValue));
462         fCurrentElement.appendChild(attributeDeclElement);
463
464         // create attribute decl
465
int attributeDeclIndex = createAttributeDecl();
466
467         // find the dataTypeValidator associcated with this attType
468
String JavaDoc attTypeString = "";
469         switch (attType) {
470         case XMLAttributeDecl.TYPE_CDATA:
471             attTypeString = "string";
472         case XMLAttributeDecl.TYPE_ENTITY:
473             attTypeString = "ENTITY";;
474         case XMLAttributeDecl.TYPE_ENUMERATION:
475             attTypeString = "ENUMERATION";;
476         case XMLAttributeDecl.TYPE_ID:
477             attTypeString = "ID";;
478         case XMLAttributeDecl.TYPE_IDREF:
479             attTypeString = "IDREF";;
480         case XMLAttributeDecl.TYPE_NMTOKEN:
481             attTypeString = "NMTOKEN";;
482         case XMLAttributeDecl.TYPE_NOTATION:
483             attTypeString = "NOTATION";;
484         default:
485             ;
486         }
487
488         // set attribute decl values
489
fAttributeDecl.clear();
490         fAttributeDecl.name.setValues(attributeDecl);
491         fAttributeDecl.type = attType;
492         fAttributeDecl.list = attList;
493         fAttributeDecl.enumeration = enumeration;
494         /***
495         fAttributeDecl.datatypeValidator =
496             DatatypeValidatorFactoryImpl.getDatatypeRegistry().getDatatypeValidator(attTypeString);
497         ****/

498         fAttributeDecl.defaultType = attDefaultType;
499         fAttributeDecl.defaultValue = fStringPool.toString(attDefaultValue);
500
501         int elementDeclIndex = getElementDeclIndex(elementDecl, -1);
502         setAttributeDecl(elementDeclIndex, attributeDeclIndex, fAttributeDecl);
503
504         int chunk = attributeDeclIndex >> CHUNK_SHIFT;
505         int index = attributeDeclIndex & CHUNK_MASK;
506         ensureAttributeDeclCapacity(chunk);
507         fAttributeDeclIsExternal[chunk][index] = isExternal ? 1 : 0;
508
509         // return index
510
return attributeDeclIndex;
511
512     } // addAttDef(QName,QName,int,int,int,int):int
513

514     /**
515      * create an XMLContentSpec for a leaf
516      *
517      * @param nameIndex StringPool handle to the name (Element) for the node
518      * @return handle to the newly create XMLContentSpec
519      * @exception java.lang.Exception
520      */

521     public int addUniqueLeafNode(int nameIndex) throws Exception JavaDoc {
522
523         // create content spec node
524
int contentSpecIndex = createContentSpec();
525
526         // set content spec node values
527
fContentSpec.setValues(XMLContentSpec.CONTENTSPECNODE_LEAF,
528                                nameIndex, -1);
529         setContentSpec(contentSpecIndex, fContentSpec);
530
531         // return index
532
return contentSpecIndex;
533
534     } // addUniqueLeafNode(int):int
535

536     /**
537      * Create an XMLContentSpec for a single non-leaf
538      *
539      * @param nodeType the type of XMLContentSpec to create - from XMLContentSpec.CONTENTSPECNODE_*
540      * @param nodeValue handle to an XMLContentSpec
541      * @return handle to the newly create XMLContentSpec
542      * @exception java.lang.Exception
543      */

544     public int addContentSpecNode(int nodeType,
545                                   int nodeValue) throws Exception JavaDoc {
546
547         // create content spec node
548
int contentSpecIndex = createContentSpec();
549
550         // set content spec node values
551
fContentSpec.setValues(nodeType, nodeValue, -1);
552         setContentSpec(contentSpecIndex, fContentSpec);
553
554         // return index
555
return contentSpecIndex;
556
557     } // addContentSpecNode(int,int):int
558

559     /**
560      * Create an XMLContentSpec for a two child leaf
561      *
562      * @param nodeType the type of XMLContentSpec to create - from XMLContentSpec.CONTENTSPECNODE_*
563      * @param leftNodeIndex handle to an XMLContentSpec
564      * @param rightNodeIndex handle to an XMLContentSpec
565      * @return handle to the newly create XMLContentSpec
566      * @exception java.lang.Exception
567      */

568     public int addContentSpecNode(int nodeType,
569                                   int leftNodeIndex,
570                                   int rightNodeIndex) throws Exception JavaDoc {
571
572         // create content spec node
573
int contentSpecIndex = createContentSpec();
574
575         // set content spec node values
576
fContentSpec.setValues(nodeType,
577                                leftNodeIndex, rightNodeIndex);
578         setContentSpec(contentSpecIndex, fContentSpec);
579
580         // return index
581
return contentSpecIndex;
582
583     } // addContentSpecNode(int,int,int):int
584

585     /**
586      * Create a string representation of an XMLContentSpec tree
587      *
588      * @param handle to an XMLContentSpec
589      * @return String representation of the content spec tree
590      * @exception java.lang.Exception
591      */

592     public String JavaDoc getContentSpecNodeAsString(int nodeIndex) throws Exception JavaDoc {
593         return XMLContentSpec.toString(this, fStringPool, nodeIndex);
594     }
595
596     /**
597      * Start the scope of an entity declaration.
598      *
599      * @return <code>true</code> on success; otherwise
600      * <code>false</code> if the entity declaration is recursive.
601      * @exception java.lang.Exception
602      */

603     public boolean startEntityDecl(boolean isPE, int entityName)
604         throws Exception JavaDoc {
605
606         // create entity decl
607
Element JavaDoc entityDecl = fGrammarDocument.createElement("entityDecl");
608         entityDecl.setAttribute("name", fStringPool.toString(entityName));
609         entityDecl.setAttribute("parameter", isPE ? "true" : "false");
610         fCurrentElement.appendChild(entityDecl);
611         fCurrentElement = entityDecl;
612
613         // success
614
return true;
615
616     } // startEntityDecl(boolean,int):boolean
617

618     /**
619      * End the scope of an entity declaration.
620      * @exception java.lang.Exception
621      */

622     public void endEntityDecl() throws Exception JavaDoc {
623
624         // get out of entity decl
625
fCurrentElement = (Element JavaDoc)fCurrentElement.getParentNode();
626
627     } // endEntityDecl()
628

629     /**
630      * Add a declaration for an internal parameter entity
631      *
632      * @param name StringPool handle of the parameter entity name
633      * @param value StringPool handle of the parameter entity value
634      * @return handle to the parameter entity declaration
635      * @exception java.lang.Exception
636      */

637     public int addInternalPEDecl(int name, int value) throws Exception JavaDoc {
638
639         // create internal PE decl
640
Element JavaDoc internalPEDecl = fGrammarDocument.createElement("internalPEDecl");
641         internalPEDecl.setAttribute("name", fStringPool.toString(name));
642         internalPEDecl.setAttribute("value", fStringPool.toString(value));
643         fCurrentElement.appendChild(internalPEDecl);
644
645         // REVISIT: What is my responsibility for creating a handle?
646
int peDeclIndex = -1;
647
648         // return index
649
return peDeclIndex;
650
651     } // addInternalPEDecl(int,int):int
652

653     /**
654      * Add a declaration for an external parameter entity
655      *
656      * @param name StringPool handle of the parameter entity name
657      * @param publicId StringPool handle of the publicId
658      * @param systemId StringPool handle of the systemId
659      * @return handle to the parameter entity declaration
660      * @exception java.lang.Exception
661      */

662     public int addExternalPEDecl(int name,
663                                  int publicId,
664                                  int systemId) throws Exception JavaDoc {
665         
666         // create external PE decl
667
Element JavaDoc externalPEDecl = fGrammarDocument.createElement("externalPEDecl");
668         externalPEDecl.setAttribute("name", fStringPool.toString(name));
669         externalPEDecl.setAttribute("publicId", fStringPool.toString(publicId));
670         externalPEDecl.setAttribute("systemId", fStringPool.toString(systemId));
671         fCurrentElement.appendChild(externalPEDecl);
672
673         // REVISIT: What is my responsibility for creating a handle?
674
int peDeclIndex = -1;
675
676         // return index
677
return peDeclIndex;
678
679     } // addExternalPEDecl(int,int,int):int
680

681     /**
682      * Add a declaration for an internal entity
683      *
684      * @param name StringPool handle of the entity name
685      * @param value StringPool handle of the entity value
686      * @return handle to the entity declaration
687      * @exception java.lang.Exception
688      */

689     public int addInternalEntityDecl(int name, int value) throws Exception JavaDoc {
690
691         // create internal entity decl
692
Element JavaDoc internalEntityDecl = fGrammarDocument.createElement("internalEntityDecl");
693         internalEntityDecl.setAttribute("name", fStringPool.toString(name));
694         internalEntityDecl.setAttribute("value", fStringPool.toString(value));
695         fCurrentElement.appendChild(internalEntityDecl);
696             
697         // REVISIT: What is my responsibility for creating a handle?
698
int internalEntityDeclIndex = -1;
699
700         // return index
701
return internalEntityDeclIndex;
702
703     } // addInternalEntityDecl(int,int):int
704

705     /**
706      * Add a declaration for an entity
707      *
708      * @param name StringPool handle of the entity name
709      * @param publicId StringPool handle of the publicId
710      * @param systemId StringPool handle of the systemId
711      * @return handle to the entity declaration
712      * @exception java.lang.Exception
713      */

714     public int addExternalEntityDecl(int name,
715                                      int publicId,
716                                      int systemId) throws Exception JavaDoc {
717
718         // create external entity decl
719
Element JavaDoc externalEntityDecl = fGrammarDocument.createElement("externalEntityDecl");
720         externalEntityDecl.setAttribute("name", fStringPool.toString(name));
721         externalEntityDecl.setAttribute("publicId", fStringPool.toString(publicId));
722         externalEntityDecl.setAttribute("systemId", fStringPool.toString(systemId));
723         fCurrentElement.appendChild(externalEntityDecl);
724             
725         // REVISIT: What is my responsibility for creating a handle?
726
int externalEntityDeclIndex = -1;
727
728         // return index
729
return externalEntityDeclIndex;
730
731     } // addExternalEntityDecl(int,int,int):int
732

733     /**
734      * Add a declaration for an unparsed entity
735      *
736      * @param name StringPool handle of the entity name
737      * @param publicId StringPool handle of the publicId
738      * @param systemId StringPool handle of the systemId
739      * @param notationName StringPool handle of the notationName
740      * @return handle to the entity declaration
741      * @exception java.lang.Exception
742      */

743     public int addUnparsedEntityDecl(int name,
744                                      int publicId, int systemId,
745                                      int notationName) throws Exception JavaDoc {
746
747         // create external entity decl
748
Element JavaDoc unparsedEntityDecl = fGrammarDocument.createElement("unparsedEntityDecl");
749         unparsedEntityDecl.setAttribute("name", fStringPool.toString(name));
750         unparsedEntityDecl.setAttribute("publicId", fStringPool.toString(publicId));
751         unparsedEntityDecl.setAttribute("systemId", fStringPool.toString(systemId));
752         unparsedEntityDecl.setAttribute("notation", fStringPool.toString(notationName));
753         fCurrentElement.appendChild(unparsedEntityDecl);
754             
755         // REVISIT: What is my responsibility for creating a handle?
756
int unparsedEntityDeclIndex = -1;
757
758         // return index
759
return unparsedEntityDeclIndex;
760
761     } // addUnparsedEntityDecl(int,int,int,int):int
762

763     /**
764      * Called when the scanner start scanning an enumeration
765      * @return StringPool handle to a string list that will hold the enumeration names
766      * @exception java.lang.Exception
767      */

768     public int startEnumeration() throws Exception JavaDoc {
769             
770         // create enumeration
771
Element JavaDoc enumeration = fGrammarDocument.createElement("enumeration");
772         fCurrentElement.appendChild(enumeration);
773         fCurrentElement = enumeration;
774
775         // REVISIT: What is my responsibility for creating a handle?
776
//int enumIndex = -1;
777
int enumIndex = fStringPool.startStringList();
778
779         // return index
780
return enumIndex;
781
782     } // startEnumeration():int
783

784     /**
785      * Add a name to an enumeration
786      * @param enumIndex StringPool handle to the string list for the enumeration
787      * @param elementType handle to the element that owns the attribute with the enumeration
788      * @param attrName StringPool handle to the name of the attribut with the enumeration
789      * @param nameIndex StringPool handle to the name to be added to the enumeration
790      * @param isNotationType true if the enumeration is an enumeration of NOTATION names
791      * @exception java.lang.Exception
792      */

793     public void addNameToEnumeration(int enumIndex,
794                                      int elementType, int attrName,
795                                      int nameIndex,
796                                      boolean isNotationType) throws Exception JavaDoc {
797         
798         // create enumeration literal
799
Element JavaDoc literal = fGrammarDocument.createElement("literal");
800         // REVISIT: How is this literal (and its parent enumeration)
801
// associated to an element and attribute name? This
802
// should be done better.
803
literal.setAttribute("element", fStringPool.toString(elementType));
804         literal.setAttribute("attribute", fStringPool.toString(attrName));
805         literal.setAttribute("name", fStringPool.toString(nameIndex));
806         literal.setAttribute("notation", isNotationType ? "true" : "false");
807         fCurrentElement.appendChild(literal);
808
809         //add the name to the stringList
810
fStringPool.addStringToList(enumIndex, nameIndex);
811
812     } // addNameToEnumeration(int,int,int,int,boolean)
813

814     /**
815      * Finish processing an enumeration
816      *
817      * @param enumIndex handle to the string list which holds the enumeration to be finshed.
818      * @exception java.lang.Exception
819      */

820     public void endEnumeration(int enumIndex) throws Exception JavaDoc {
821
822         // get out of enumeration
823
fCurrentElement = (Element JavaDoc)fCurrentElement.getParentNode();
824         
825         //finish the enumeration stringlist int the fStringPool
826
fStringPool.finishStringList(enumIndex);
827
828     } // endEnumeration(int)
829

830     /**
831      * Add a declaration for a notation
832      *
833      * @param notationName
834      * @param publicId
835      * @param systemId
836      * @return handle to the notation declaration
837      * @exception java.lang.Exception
838      */

839     public int addNotationDecl(int notationName,
840                                int publicId, int systemId) throws Exception JavaDoc {
841
842         // create notation decl
843
Element JavaDoc notationDecl = fGrammarDocument.createElement("notationDecl");
844         notationDecl.setAttribute("name", fStringPool.toString(notationName));
845         notationDecl.setAttribute("publicId", fStringPool.toString(publicId));
846         notationDecl.setAttribute("systemId", fStringPool.toString(systemId));
847         fCurrentElement.appendChild(notationDecl);
848
849         // REVISIT: What is my responsibility for creating a handle?
850
int notationDeclIndex = -1;
851
852         // return index
853
return notationDeclIndex;
854
855     } // addNotationdecl(int,int,int):int
856

857     /**
858      * Called when a comment has been scanned
859      *
860      * @param data StringPool handle of the comment text
861      * @exception java.lang.Exception
862      */

863     public void callComment(int data) throws Exception JavaDoc {
864     }
865
866     /**
867      * Called when a processing instruction has been scanned
868      * @param piTarget StringPool handle of the PI target
869      * @param piData StringPool handle of the PI data
870      * @exception java.lang.Exception
871      */

872     public void callProcessingInstruction(int piTarget, int piData)
873         throws Exception JavaDoc {
874
875         // create pi
876
ProcessingInstruction JavaDoc pi =
877             fGrammarDocument.createProcessingInstruction(fStringPool.toString(piTarget),
878                                                          fStringPool.toString(piData));
879         fCurrentElement.appendChild(pi);
880
881     } // callProcessingInstruction(int,int)
882

883     // deprecated -- removed from DOM Level 2
884

885     /**
886      * Supports DOM Level 2 internalSubset additions.
887      * Called when the internal subset is completely scanned.
888      */

889     public void internalSubset(int internalSubset) throws Exception JavaDoc {
890     }
891
892     protected boolean isDTD() {
893         return true;
894     }
895
896     //
897
// Private methods
898
//
899

900     // ensure capacity
901

902     /** Ensures storage for element declaration mappings. */
903     private void ensureElementDeclCapacity(int chunk) {
904         if (chunk >= fElementDeclMap.length) {
905             fElementDeclMap = resize(fElementDeclMap,
906                                      fElementDeclMap.length * 2);
907             fElementDeclIsExternal = resize(fElementDeclIsExternal,
908                                      fElementDeclIsExternal.length * 2);
909         } else if (fElementDeclMap[chunk] != null) {
910             return;
911         }
912         fElementDeclMap[chunk] = new int[CHUNK_SIZE];
913         fElementDeclIsExternal[chunk] = new int[CHUNK_SIZE];
914     }
915
916     /** Ensures storage for attribute declaration mappings. */
917     private void ensureAttributeDeclCapacity(int chunk) {
918         if (chunk >= fAttributeDeclMap.length) {
919             fAttributeDeclMap = resize(fAttributeDeclMap,
920                                        fAttributeDeclMap.length * 2);
921             fAttributeDeclIsExternal = resize(fAttributeDeclIsExternal,
922                                        fAttributeDeclIsExternal.length * 2);
923         } else if (fAttributeDeclMap[chunk] != null) {
924             return;
925         }
926         fAttributeDeclMap[chunk] = new int[CHUNK_SIZE];
927         fAttributeDeclIsExternal[chunk] = new int[CHUNK_SIZE];
928     }
929
930     /** Ensures storage for content spec mappings. */
931     private void ensureContentSpecCapacity(int chunk) {
932         if (chunk >= fContentSpecMap.length) {
933             fContentSpecMap = resize(fContentSpecMap,
934                                      fContentSpecMap.length * 2);
935         } else if (fContentSpecMap[chunk] != null) {
936             return;
937         }
938         fContentSpecMap[chunk] = new int[CHUNK_SIZE];
939     }
940
941     // resize initial chunk
942

943     /** Resizes chunked integer arrays. */
944     private int[][] resize(int array[][], int newsize) {
945         int newarray[][] = new int[newsize][];
946         System.arraycopy(array, 0, newarray, 0, array.length);
947         return newarray;
948     }
949
950 } // class DTDGrammar
951
Popular Tags