KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thaiopensource > relaxng > impl > IdSoundnessChecker


1 package com.thaiopensource.relaxng.impl;
2
3 import com.thaiopensource.validate.Validator;
4 import com.thaiopensource.xml.util.StringSplitter;
5 import com.thaiopensource.xml.util.Name;
6 import org.relaxng.datatype.Datatype;
7 import org.xml.sax.Attributes JavaDoc;
8 import org.xml.sax.ErrorHandler JavaDoc;
9 import org.xml.sax.Locator JavaDoc;
10 import org.xml.sax.SAXException JavaDoc;
11 import org.xml.sax.SAXParseException JavaDoc;
12 import org.xml.sax.ContentHandler JavaDoc;
13 import org.xml.sax.DTDHandler JavaDoc;
14 import org.xml.sax.helpers.LocatorImpl JavaDoc;
15
16 import java.util.Enumeration JavaDoc;
17 import java.util.Hashtable JavaDoc;
18 import java.util.Vector JavaDoc;
19
20 public class IdSoundnessChecker implements Validator, ContentHandler JavaDoc {
21   private final IdTypeMap idTypeMap;
22   private final ErrorHandler JavaDoc eh;
23   private Locator JavaDoc locator;
24   private final Hashtable JavaDoc table = new Hashtable JavaDoc();
25
26   private static class Entry {
27     Locator JavaDoc idLoc;
28     Vector JavaDoc idrefLocs;
29     boolean hadId;
30   }
31
32   public IdSoundnessChecker(IdTypeMap idTypeMap, ErrorHandler JavaDoc eh) {
33     this.idTypeMap = idTypeMap;
34     this.eh = eh;
35   }
36
37   public void reset() {
38     table.clear();
39     locator = null;
40   }
41
42   public ContentHandler JavaDoc getContentHandler() {
43     return this;
44   }
45
46   public DTDHandler JavaDoc getDTDHandler() {
47     return null;
48   }
49
50   public void setDocumentLocator(Locator JavaDoc locator) {
51     this.locator = locator;
52   }
53
54   public void startDocument() throws SAXException JavaDoc {
55   }
56
57   void setComplete() {
58   }
59
60   public void endDocument() throws SAXException JavaDoc {
61     for (Enumeration JavaDoc e = table.keys(); e.hasMoreElements();) {
62       String JavaDoc token = (String JavaDoc)e.nextElement();
63       Entry entry = (Entry)table.get(token);
64       if (!entry.hadId) {
65         for (Enumeration JavaDoc f = entry.idrefLocs.elements(); f.hasMoreElements();)
66           error("missing_id", token, (Locator JavaDoc)f.nextElement());
67       }
68     }
69     setComplete();
70   }
71
72   public void startPrefixMapping(String JavaDoc s, String JavaDoc s1) throws SAXException JavaDoc {
73   }
74
75   public void endPrefixMapping(String JavaDoc s) throws SAXException JavaDoc {
76   }
77
78   public void startElement(String JavaDoc namespaceUri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes)
79           throws SAXException JavaDoc {
80     Name elementName = new Name(namespaceUri, localName);
81     int len = attributes.getLength();
82     for (int i = 0; i < len; i++) {
83       Name attributeName = new Name(attributes.getURI(i), attributes.getLocalName(i));
84       int idType = idTypeMap.getIdType(elementName, attributeName);
85       if (idType != Datatype.ID_TYPE_NULL) {
86         String JavaDoc[] tokens = StringSplitter.split(attributes.getValue(i));
87         switch (idType) {
88         case Datatype.ID_TYPE_ID:
89           if (tokens.length == 1)
90             id(tokens[0]);
91           else if (tokens.length == 0)
92             error("id_no_tokens");
93           else
94             error("id_multiple_tokens");
95           break;
96         case Datatype.ID_TYPE_IDREF:
97           if (tokens.length == 1)
98             idref(tokens[0]);
99           else if (tokens.length == 0)
100             error("idref_no_tokens");
101           else
102             error("idref_multiple_tokens");
103           break;
104         case Datatype.ID_TYPE_IDREFS:
105           if (tokens.length > 0) {
106             for (int j = 0; j < tokens.length; j++)
107               idref(tokens[j]);
108           }
109           else
110             error("idrefs_no_tokens");
111           break;
112         }
113       }
114     }
115   }
116
117   private void id(String JavaDoc token) throws SAXException JavaDoc {
118     Entry entry = (Entry)table.get(token);
119     if (entry == null) {
120       entry = new Entry();
121       table.put(token, entry);
122     }
123     else if (entry.hadId) {
124       error("duplicate_id", token);
125       error("first_id", token, entry.idLoc);
126       return;
127     }
128     entry.idLoc = new LocatorImpl JavaDoc(locator);
129     entry.hadId = true;
130   }
131
132   private void idref(String JavaDoc token) {
133     Entry entry = (Entry)table.get(token);
134     if (entry == null) {
135       entry = new Entry();
136       table.put(token, entry);
137     }
138     if (entry.hadId)
139       return;
140     if (entry.idrefLocs == null)
141       entry.idrefLocs = new Vector JavaDoc();
142     entry.idrefLocs.addElement(new LocatorImpl JavaDoc(locator));
143   }
144
145   public void endElement(String JavaDoc s, String JavaDoc s1, String JavaDoc s2) throws SAXException JavaDoc {
146   }
147
148   public void characters(char[] chars, int i, int i1) throws SAXException JavaDoc {
149   }
150
151   public void ignorableWhitespace(char[] chars, int i, int i1) throws SAXException JavaDoc {
152   }
153
154   public void processingInstruction(String JavaDoc s, String JavaDoc s1) throws SAXException JavaDoc {
155   }
156
157   public void skippedEntity(String JavaDoc s) throws SAXException JavaDoc {
158   }
159
160   public void notationDecl(String JavaDoc name,
161                            String JavaDoc publicId,
162                            String JavaDoc systemId)
163           throws SAXException JavaDoc {
164   }
165
166   public void unparsedEntityDecl(String JavaDoc name,
167                                  String JavaDoc publicId,
168                                  String JavaDoc systemId,
169                                  String JavaDoc notationName)
170           throws SAXException JavaDoc {
171   }
172
173   private void error(String JavaDoc key) throws SAXException JavaDoc {
174     eh.error(new SAXParseException JavaDoc(SchemaBuilderImpl.localizer.message(key), locator));
175   }
176
177   private void error(String JavaDoc key, String JavaDoc arg) throws SAXException JavaDoc {
178     eh.error(new SAXParseException JavaDoc(SchemaBuilderImpl.localizer.message(key, arg), locator));
179   }
180
181   private void error(String JavaDoc key, String JavaDoc arg, Locator JavaDoc loc) throws SAXException JavaDoc {
182     eh.error(new SAXParseException JavaDoc(SchemaBuilderImpl.localizer.message(key, arg),
183                                    loc));
184   }
185 }
186
Popular Tags