KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > convert > xml > AuthConstraintImpl


1 /**
2  * This class was generated from a set of XML constraints
3  * by the Enhydra Zeus XML Data Binding Framework. All
4  * source code in this file is constructed specifically
5  * to work with other Zeus-generated classes. If you
6  * modify this file by hand, you run the risk of breaking
7  * this interoperation, as well as introducing errors in
8  * source code compilation.
9  *
10  * * * * * MODIFY THIS FILE AT YOUR OWN RISK * * * * *
11  *
12  * To find out more about the Enhydra Zeus framework, you
13  * can point your browser at <http://zeus.enhydra.org>
14  * where you can download releases, join and discuss Zeus
15  * on user and developer mailing lists, and access source
16  * code. Please report any bugs through that website.
17  */

18 package org.enhydra.convert.xml;
19
20 // Global Implementation Import Statements
21
import java.io.File JavaDoc;
22 import java.io.FileReader JavaDoc;
23 import java.io.FileWriter JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.InputStreamReader JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.io.OutputStreamWriter JavaDoc;
29 import java.io.Reader JavaDoc;
30 import java.io.Writer JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Map JavaDoc;
34 import org.xml.sax.EntityResolver JavaDoc;
35 import org.xml.sax.ErrorHandler JavaDoc;
36 import org.xml.sax.InputSource JavaDoc;
37 import org.xml.sax.Locator JavaDoc;
38 import org.xml.sax.SAXException JavaDoc;
39 import org.xml.sax.SAXParseException JavaDoc;
40 import org.xml.sax.XMLReader JavaDoc;
41 import org.xml.sax.ext.LexicalHandler JavaDoc;
42 import org.xml.sax.helpers.DefaultHandler JavaDoc;
43 import org.xml.sax.helpers.XMLReaderFactory JavaDoc;
44
45 // Local Implementation Import Statements
46
import java.util.Iterator JavaDoc;
47 import java.util.List JavaDoc;
48 import java.util.LinkedList JavaDoc;
49
50 public class AuthConstraintImpl extends DefaultHandler JavaDoc implements Cloneable JavaDoc, Unmarshallable, LexicalHandler JavaDoc, AuthConstraint {
51
52     private Description description;
53     private List JavaDoc roleNameList;
54     private String JavaDoc id;
55     private boolean zeus_IdSet;
56
57     /** Any DOCTYPE reference/statements. */
58     private String JavaDoc docTypeString;
59
60     /** The encoding for the output document */
61     private String JavaDoc outputEncoding;
62
63     /** The current node in unmarshalling */
64     private Unmarshallable zeus_currentUNode;
65
66     /** The parent node in unmarshalling */
67     private Unmarshallable zeus_parentUNode;
68
69     /** Whether this node has been handled */
70     private boolean zeus_thisNodeHandled = false;
71
72     /** Whether a DTD exists for an unmarshal call */
73     private boolean hasDTD;
74
75     /** Whether validation is occurring */
76     private boolean validate;
77
78     /** The namespace mappings on this element */
79     private Map JavaDoc namespaceMappings;
80
81     /** The EntityResolver for SAX parsing to use */
82     private static EntityResolver JavaDoc entityResolver;
83
84     /** The ErrorHandler for SAX parsing to use */
85     private static ErrorHandler JavaDoc errorHandler;
86
87     private static AuthConstraintImpl prototype = null;
88
89     public static void setPrototype(AuthConstraintImpl prototype) {
90         AuthConstraintImpl.prototype = prototype;
91     }
92     public static AuthConstraintImpl newInstance() {
93         try {
94             return (prototype!=null)?(AuthConstraintImpl)prototype.clone(): new AuthConstraintImpl();
95         } catch (CloneNotSupportedException JavaDoc e) {
96             return null; // never
97
}
98     }
99     public AuthConstraintImpl() {
100         roleNameList = new LinkedList JavaDoc();
101         zeus_IdSet = false;
102         docTypeString = "";
103         hasDTD = false;
104         validate = false;
105         namespaceMappings = new HashMap JavaDoc();
106     }
107
108     public Description getDescription() {
109         return description;
110     }
111
112     public void setDescription(Description description) {
113         this.description = description;
114     }
115
116     public List JavaDoc getRoleNameList() {
117         return roleNameList;
118     }
119
120     public void setRoleNameList(List JavaDoc roleNameList) {
121         this.roleNameList = roleNameList;
122     }
123
124     public void addRoleName(RoleName roleName) {
125         roleNameList.add(roleName);
126     }
127
128     public void removeRoleName(RoleName roleName) {
129         roleNameList.remove(roleName);
130     }
131
132     public String JavaDoc getId() {
133         return id;
134     }
135
136     public void setId(String JavaDoc id) {
137         this.id = id;
138         zeus_IdSet = true;
139     }
140
141     public void setDocType(String JavaDoc name, String JavaDoc publicID, String JavaDoc systemID) {
142         try {
143             startDTD(name, publicID, systemID);
144         } catch (SAXException JavaDoc neverHappens) { }
145     }
146
147     public void setOutputEncoding(String JavaDoc outputEncoding) {
148         this.outputEncoding = outputEncoding;
149     }
150
151     public void marshal(File JavaDoc file) throws IOException JavaDoc {
152         // Delegate to the marshal(Writer) method
153
marshal(new FileWriter JavaDoc(file));
154     }
155
156     public void marshal(OutputStream JavaDoc outputStream) throws IOException JavaDoc {
157         // Delegate to the marshal(Writer) method
158
marshal(new OutputStreamWriter JavaDoc(outputStream));
159     }
160
161     public void marshal(Writer JavaDoc writer) throws IOException JavaDoc {
162         // Write out the XML declaration
163
writer.write("<?xml version=\"1.0\" ");
164         if (outputEncoding != null) {
165             writer.write("encoding=\"");
166             writer.write(outputEncoding);
167             writer.write("\"?>\n\n");
168
169         } else {
170             writer.write("encoding=\"UTF-8\"?>\n\n");
171
172         }
173         // Handle DOCTYPE declaration
174
writer.write(docTypeString);
175         writer.write("\n");
176         // Now start the recursive writing
177
writeXMLRepresentation(writer, "");
178
179         // Close up
180
writer.flush();
181         writer.close();
182     }
183
184     protected void writeXMLRepresentation(Writer JavaDoc writer,
185                                           String JavaDoc indent)
186         throws IOException JavaDoc {
187
188         writer.write(indent);
189         writer.write("<auth-constraint");
190
191         // Handle namespace mappings (if needed)
192
for (Iterator JavaDoc i = namespaceMappings.keySet().iterator(); i.hasNext(); ) {
193             String JavaDoc prefix = (String JavaDoc)i.next();
194             String JavaDoc uri = (String JavaDoc)namespaceMappings.get(prefix);
195             writer.write(" xmlns");
196             if (!prefix.trim().equals("")) {
197                 writer.write(":");
198                 writer.write(prefix);
199             }
200             writer.write("=\"");
201             writer.write(uri);
202             writer.write("\"\n ");
203         }
204
205         // Handle attributes (if needed)
206
if (zeus_IdSet) {
207             writer.write(" id=\"");
208             writer.write(escapeAttributeValue(id));
209             writer.write("\"");
210         }
211         writer.write(">");
212         writer.write("\n");
213
214         // Handle child elements
215
if (description != null) {
216             ((DescriptionImpl)description).writeXMLRepresentation(writer,
217                 new StringBuffer JavaDoc(indent).append(" ").toString());
218         }
219
220         for (Iterator JavaDoc i=roleNameList.iterator(); i.hasNext(); ) {
221             RoleNameImpl roleName = (RoleNameImpl)i.next();
222             roleName.writeXMLRepresentation(writer,
223                 new StringBuffer JavaDoc(indent).append(" ").toString());
224         }
225         writer.write(indent);
226         writer.write("</auth-constraint>\n");
227     }
228
229     private String JavaDoc escapeAttributeValue(String JavaDoc attributeValue) {
230         String JavaDoc returnValue = attributeValue;
231         for (int i = 0; i < returnValue.length(); i++) {
232             char ch = returnValue.charAt(i);
233             if (ch == '"') {
234                 returnValue = new StringBuffer JavaDoc()
235                     .append(returnValue.substring(0, i))
236                     .append("&quot;")
237                     .append(returnValue.substring(i+1))
238                     .toString();
239             }
240         }
241         return returnValue;
242     }
243
244     private String JavaDoc escapeTextValue(String JavaDoc textValue) {
245         String JavaDoc returnValue = textValue;
246         for (int i = 0; i < returnValue.length(); i++) {
247             char ch = returnValue.charAt(i);
248             if (ch == '<') {
249                 returnValue = new StringBuffer JavaDoc()
250                     .append(returnValue.substring(0, i))
251                     .append("&lt;")
252                     .append(returnValue.substring(i+1))
253                     .toString();
254             } else if (ch == '>') {
255                 returnValue = new StringBuffer JavaDoc()
256                     .append(returnValue.substring(0, i))
257                     .append("&gt;")
258                     .append(returnValue.substring(i+1))
259                     .toString();
260             }
261         }
262         return returnValue;
263     }
264
265     /**
266      * <p>
267      * This sets a SAX <code>EntityResolver</code> for this unmarshalling process.
268      * </p>
269      *
270      * @param resolver the entity resolver to use.
271      */

272     public static void setEntityResolver(EntityResolver JavaDoc resolver) {
273         entityResolver = resolver;
274     }
275
276     /**
277      * <p>
278      * This sets a SAX <code>ErrorHandler</code> for this unmarshalling process.
279      * </p>
280      *
281      * @param handler the entity resolver to use.
282      */

283     public static void setErrorHandler(ErrorHandler JavaDoc handler) {
284         errorHandler = handler;
285     }
286
287     public static AuthConstraint unmarshal(File JavaDoc file) throws IOException JavaDoc {
288         // Delegate to the unmarshal(Reader) method
289
return unmarshal(new FileReader JavaDoc(file));
290     }
291
292     public static AuthConstraint unmarshal(File JavaDoc file, boolean validate) throws IOException JavaDoc {
293         // Delegate to the unmarshal(Reader) method
294
return unmarshal(new FileReader JavaDoc(file), validate);
295     }
296
297     public static AuthConstraint unmarshal(InputStream JavaDoc inputStream) throws IOException JavaDoc {
298         // Delegate to the unmarshal(Reader) method
299
return unmarshal(new InputStreamReader JavaDoc(inputStream));
300     }
301
302     public static AuthConstraint unmarshal(InputStream JavaDoc inputStream, boolean validate) throws IOException JavaDoc {
303         // Delegate to the unmarshal(Reader) method
304
return unmarshal(new InputStreamReader JavaDoc(inputStream), validate);
305     }
306
307     public static AuthConstraint unmarshal(Reader JavaDoc reader) throws IOException JavaDoc {
308         // Delegate with default validation value
309
return unmarshal(reader, false);
310     }
311
312     public static AuthConstraint unmarshal(Reader JavaDoc reader, boolean validate) throws IOException JavaDoc {
313         AuthConstraintImpl authConstraint = AuthConstraintImpl.newInstance();
314         authConstraint.setValidating(validate);
315         authConstraint.setCurrentUNode(authConstraint);
316         authConstraint.setParentUNode(null);
317         // Load the XML parser
318
XMLReader JavaDoc parser = null;
319         String JavaDoc parserClass = System.getProperty("org.xml.sax.driver",
320             "org.apache.xerces.parsers.SAXParser");
321         try {
322             parser = XMLReaderFactory.createXMLReader(parserClass);
323
324             // Set entity resolver, if needed
325
if (entityResolver != null) {
326                 parser.setEntityResolver(entityResolver);
327             }
328
329             // Set error handler
330
parser.setErrorHandler(authConstraint);
331
332             // Register lexical handler
333
parser.setProperty("http://xml.org/sax/properties/lexical-handler", authConstraint);
334
335             // Register content handler
336
parser.setContentHandler(authConstraint);
337         } catch (SAXException JavaDoc e) {
338             throw new IOException JavaDoc("Could not load XML parser: " +
339                 e.getMessage());
340         }
341
342         InputSource JavaDoc inputSource = new InputSource JavaDoc(reader);
343         try {
344             parser.setFeature("http://xml.org/sax/features/validation", new Boolean JavaDoc(validate).booleanValue());
345             parser.setFeature("http://xml.org/sax/features/namespaces", true);
346             parser.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
347             parser.parse(inputSource);
348         } catch (SAXException JavaDoc e) {
349             throw new IOException JavaDoc("Error parsing XML document: " +
350                 e.getMessage());
351         }
352
353         // Return the resultant object
354
return authConstraint;
355     }
356
357     public Unmarshallable getParentUNode() {
358         return zeus_parentUNode;
359     }
360
361     public void setParentUNode(Unmarshallable parentUNode) {
362         this.zeus_parentUNode = parentUNode;
363     }
364
365     public Unmarshallable getCurrentUNode() {
366         return zeus_currentUNode;
367     }
368
369     public void setCurrentUNode(Unmarshallable currentUNode) {
370         this.zeus_currentUNode = currentUNode;
371     }
372
373     public void setValidating(boolean validate) {
374         this.validate = validate;
375     }
376
377     public void startDocument() throws SAXException JavaDoc {
378         // no-op
379
}
380
381     public void setDocumentLocator(Locator JavaDoc locator) {
382         // no-op
383
}
384
385     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
386         throws SAXException JavaDoc {
387         namespaceMappings.put(prefix, uri);
388     }
389
390     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName,
391                              String JavaDoc qName, org.xml.sax.Attributes JavaDoc atts)
392         throws SAXException JavaDoc {
393
394         // Feed this to the correct ContentHandler
395
Unmarshallable current = getCurrentUNode();
396         if (current != this) {
397             current.startElement(namespaceURI, localName, qName, atts);
398             return;
399         }
400
401         // See if we handle, or we delegate
402
if ((localName.equals("auth-constraint")) && (!zeus_thisNodeHandled)) {
403             // Handle ourselves
404
for (int i=0, len=atts.getLength(); i<len; i++) {
405                 String JavaDoc attName= atts.getLocalName(i);
406                 String JavaDoc attValue = atts.getValue(i);
407                 if (attName.equals("id")) {
408                     setId(attValue);
409                 }
410             }
411             zeus_thisNodeHandled = true;
412             return;
413         } else {
414             // Delegate handling
415
if (localName.equals("description") && (description==null)) {
416                 DescriptionImpl description = DescriptionImpl.newInstance();
417                 current = getCurrentUNode();
418                 description.setParentUNode(current);
419                 description.setCurrentUNode(description);
420                 this.setCurrentUNode(description);
421                 description.startElement(namespaceURI, localName, qName, atts);
422                 // Add this value in
423
this.description = description;
424                 return;
425             }
426             if (localName.equals("role-name")) {
427                 RoleNameImpl roleName = RoleNameImpl.newInstance();
428                 current = getCurrentUNode();
429                 roleName.setParentUNode(current);
430                 roleName.setCurrentUNode(roleName);
431                 this.setCurrentUNode(roleName);
432                 roleName.startElement(namespaceURI, localName, qName, atts);
433                 // Add this value in
434
roleNameList.add(roleName);
435                 return;
436             }
437         }
438     }
439
440     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName,
441                            String JavaDoc qName)
442         throws SAXException JavaDoc {
443
444         Unmarshallable current = getCurrentUNode();
445         if (current != this) {
446             current.endElement(namespaceURI, localName, qName);
447             return;
448         }
449
450         Unmarshallable parent = getCurrentUNode().getParentUNode();
451         if (parent != null) {
452             parent.setCurrentUNode(parent);
453         }
454     }
455
456     public void characters(char[] ch, int start, int len)
457         throws SAXException JavaDoc {
458
459         // Feed this to the correct ContentHandler
460
Unmarshallable current = getCurrentUNode();
461         if (current != this) {
462             current.characters(ch, start, len);
463             return;
464         }
465
466         String JavaDoc text = new String JavaDoc(ch, start, len);
467     }
468
469     public void comment(char ch[], int start, int len) throws SAXException JavaDoc {
470         // Currently no-op
471
}
472
473     public void warning(SAXParseException JavaDoc e) throws SAXException JavaDoc {
474         if (errorHandler != null) {
475             errorHandler.warning(e);
476         }
477     }
478
479     public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc {
480         if ((validate) && (!hasDTD)) {
481             throw new SAXException JavaDoc("Validation is turned on, but no DTD has been specified in the input XML document. Please supply a DTD through a DOCTYPE reference.");
482         }
483         if (errorHandler != null) {
484             errorHandler.error(e);
485         }
486     }
487
488     public void fatalError(SAXParseException JavaDoc e) throws SAXException JavaDoc {
489         if ((validate) && (!hasDTD)) {
490             throw new SAXException JavaDoc("Validation is turned on, but no DTD has been specified in the input XML document. Please supply a DTD through a DOCTYPE reference.");
491         }
492         if (errorHandler != null) {
493             errorHandler.fatalError(e);
494         }
495     }
496
497     public void startDTD(String JavaDoc name, String JavaDoc publicID, String JavaDoc systemID)
498         throws SAXException JavaDoc {
499
500         if ((name == null) || (name.equals(""))) {
501             docTypeString = "";
502             return;
503         }
504
505         hasDTD = true;
506         StringBuffer JavaDoc docTypeSB = new StringBuffer JavaDoc();
507         boolean hasPublic = false;
508
509         docTypeSB.append("<!DOCTYPE ")
510                  .append(name);
511
512         if ((publicID != null) && (!publicID.equals(""))) {
513             docTypeSB.append(" PUBLIC \"")
514                      .append(publicID)
515                      .append("\"");
516             hasPublic = true;
517         }
518
519         if ((systemID != null) && (!systemID.equals(""))) {
520             if (!hasPublic) {
521                 docTypeSB.append(" SYSTEM");
522             }
523             docTypeSB.append(" \"")
524                      .append(systemID)
525                      .append("\"");
526
527         }
528
529         docTypeSB.append(">");
530
531         docTypeString = docTypeSB.toString();
532     }
533
534     public void endDTD() throws SAXException JavaDoc {
535         // Currently no-op
536
}
537
538     public void startEntity(String JavaDoc name) throws SAXException JavaDoc {
539         // Currently no-op
540
}
541
542     public void endEntity(String JavaDoc name) throws SAXException JavaDoc {
543         // Currently no-op
544
}
545
546     public void startCDATA() throws SAXException JavaDoc {
547         // Currently no-op
548
}
549
550     public void endCDATA() throws SAXException JavaDoc {
551         // Currently no-op
552
}
553
554 }
555
Popular Tags