KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > schema > loader > ContentModelHandler


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.schema.loader;
24
25 import org.xml.sax.Attributes JavaDoc;
26 import org.xml.sax.SAXException JavaDoc;
27 import org.xquark.schema.*;
28 import org.xquark.util.DefaultElementHandler;
29 import org.xquark.util.ElementHandler;
30
31 class ContentModelHandler extends DefaultElementHandler implements SchemaConstants
32 {
33 private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
34 private static final String JavaDoc RCSName = "$Name: $";
35     private Loader loader = null;
36     private ModelGroup group = null;
37     private SchemaScope scope = null;
38     private ModelGroupDefinition groupDef = null;
39   private boolean redefined = false;
40
41     ContentModelHandler(Loader loader, ModelGroupDefinition def, boolean redefined) {
42         this.loader = loader;
43         groupDef = def;
44         this.redefined = redefined;
45     if ( redefined ) {
46       group = new SequenceModelGroup();
47       def.setModelGroup(group);
48     }
49     }
50
51     ContentModelHandler(Loader loader, ModelGroup group, SchemaScope scope) {
52         this.loader = loader;
53         this.group = group;
54         this.scope = scope;
55     }
56     
57     public ElementHandler startElement(String JavaDoc namespaceURI, String JavaDoc localName, Attributes JavaDoc atts)
58     throws SAXException JavaDoc
59     {
60       if ( namespaceURI.equals(XMLSCHEMA_URI) ) {
61         // the first part : redefine only
62
if ( redefined && localName.equals(GROUP_TAG)) {
63           // check self reference
64
String JavaDoc refName = atts.getValue("", REF_ATTR);
65           // refName == null is verificated by Schema of Schemas
66
if ( refName.equals(groupDef.getName()) ) {
67             // self reference
68
// check minOccurs, maxOccurs
69
String JavaDoc minOccurs = atts.getValue("", MIN_OCCURS_ATTR);
70             String JavaDoc maxOccurs = atts.getValue("", MAX_OCCURS_ATTR);
71             if ( ( minOccurs != null && !"1".equals(minOccurs.trim()) ) ||
72                  ( maxOccurs != null && !"1".equals(maxOccurs.trim()) ) ) {
73               String JavaDoc errCode = "src-redefine.6.1.2";
74               String JavaDoc errMsg = "Error while processing localName -> " + localName;
75               loader.reportLoadingError(errMsg, new SchemaException(errCode));
76             }
77             String JavaDoc redefinedName = refName + ".redefine";
78             ModelGroupDefinition redefinedGroup = loader.getSchema().getModelGroupDefinition(redefinedName);
79             Particle result = new Particle(redefinedGroup);
80             result.setMinOccurs(1);
81             result.setMaxOccurs(1);
82             group.add(result);
83             return this;
84           }
85         }
86
87         Particle particle = null;
88         try {
89           particle = loader.buildParticle(localName, scope, atts);
90         }
91         catch ( SchemaException se ) {
92           String JavaDoc errMsg = "Error while processing localName -> " + localName;
93           loader.reportLoadingError(errMsg, se);
94         }
95         if (particle != null) {
96           if (group != null) group.add(particle);
97           Object JavaDoc term = particle.getTerm();
98           if (term instanceof ModelGroup) {
99             if (group == null) {
100               group = (ModelGroup)term;
101               groupDef.setModelGroup(group);
102             }
103             return new ContentModelHandler(loader, (ModelGroup)term, scope);
104           }
105           else if (term instanceof ElementDeclaration) {
106             return new ElementTypeHandler(loader, (ElementDeclaration)term);
107           }
108           else {
109             return this;
110           }
111         }
112
113         else if (localName.equals(ANNOTATION_TAG)) {
114           return new AnnotationHandler();
115         }
116         else
117           // modefied by ZL, 26,septembre 2001
118
// particle can be null in the case of minOccurs=maxOccurs=0
119
return this;
120       }
121       
122       loader.notifyUnknownElement(namespaceURI, localName);
123       return this;
124     }
125 }
126
Popular Tags