001: /**
002: * This class was generated from a set of XML constraints
003: * by the Enhydra Zeus XML Data Binding Framework. All
004: * source code in this file is constructed specifically
005: * to work with other Zeus-generated classes. If you
006: * modify this file by hand, you run the risk of breaking
007: * this interoperation, as well as introducing errors in
008: * source code compilation.
009: *
010: * * * * * MODIFY THIS FILE AT YOUR OWN RISK * * * * *
011: *
012: * To find out more about the Enhydra Zeus framework, you
013: * can point your browser at <http://zeus.enhydra.org>
014: * where you can download releases, join and discuss Zeus
015: * on user and developer mailing lists, and access source
016: * code. Please report any bugs through that website.
017: */package org.enhydra.convert.xml;
018:
019: // Global Implementation Import Statements
020: import java.io.File;
021: import java.io.FileReader;
022: import java.io.FileWriter;
023: import java.io.InputStream;
024: import java.io.InputStreamReader;
025: import java.io.IOException;
026: import java.io.OutputStream;
027: import java.io.OutputStreamWriter;
028: import java.io.Reader;
029: import java.io.Writer;
030: import java.util.HashMap;
031: import java.util.Iterator;
032: import java.util.Map;
033: import org.xml.sax.EntityResolver;
034: import org.xml.sax.ErrorHandler;
035: import org.xml.sax.InputSource;
036: import org.xml.sax.Locator;
037: import org.xml.sax.SAXException;
038: import org.xml.sax.SAXParseException;
039: import org.xml.sax.XMLReader;
040: import org.xml.sax.ext.LexicalHandler;
041: import org.xml.sax.helpers.DefaultHandler;
042: import org.xml.sax.helpers.XMLReaderFactory;
043:
044: public class SecurityRoleImpl extends DefaultHandler implements
045: Cloneable, Unmarshallable, LexicalHandler, SecurityRole {
046:
047: private Description description;
048: private RoleName roleName;
049: private String id;
050: private boolean zeus_IdSet;
051:
052: /** Any DOCTYPE reference/statements. */
053: private String docTypeString;
054:
055: /** The encoding for the output document */
056: private String outputEncoding;
057:
058: /** The current node in unmarshalling */
059: private Unmarshallable zeus_currentUNode;
060:
061: /** The parent node in unmarshalling */
062: private Unmarshallable zeus_parentUNode;
063:
064: /** Whether this node has been handled */
065: private boolean zeus_this NodeHandled = false;
066:
067: /** Whether a DTD exists for an unmarshal call */
068: private boolean hasDTD;
069:
070: /** Whether validation is occurring */
071: private boolean validate;
072:
073: /** The namespace mappings on this element */
074: private Map namespaceMappings;
075:
076: /** The EntityResolver for SAX parsing to use */
077: private static EntityResolver entityResolver;
078:
079: /** The ErrorHandler for SAX parsing to use */
080: private static ErrorHandler errorHandler;
081:
082: private static SecurityRoleImpl prototype = null;
083:
084: public static void setPrototype(SecurityRoleImpl prototype) {
085: SecurityRoleImpl.prototype = prototype;
086: }
087:
088: public static SecurityRoleImpl newInstance() {
089: try {
090: return (prototype != null) ? (SecurityRoleImpl) prototype
091: .clone() : new SecurityRoleImpl();
092: } catch (CloneNotSupportedException e) {
093: return null; // never
094: }
095: }
096:
097: public SecurityRoleImpl() {
098: zeus_IdSet = false;
099: docTypeString = "";
100: hasDTD = false;
101: validate = false;
102: namespaceMappings = new HashMap();
103: }
104:
105: public Description getDescription() {
106: return description;
107: }
108:
109: public void setDescription(Description description) {
110: this .description = description;
111: }
112:
113: public RoleName getRoleName() {
114: return roleName;
115: }
116:
117: public void setRoleName(RoleName roleName) {
118: this .roleName = roleName;
119: }
120:
121: public String getId() {
122: return id;
123: }
124:
125: public void setId(String id) {
126: this .id = id;
127: zeus_IdSet = true;
128: }
129:
130: public void setDocType(String name, String publicID, String systemID) {
131: try {
132: startDTD(name, publicID, systemID);
133: } catch (SAXException neverHappens) {
134: }
135: }
136:
137: public void setOutputEncoding(String outputEncoding) {
138: this .outputEncoding = outputEncoding;
139: }
140:
141: public void marshal(File file) throws IOException {
142: // Delegate to the marshal(Writer) method
143: marshal(new FileWriter(file));
144: }
145:
146: public void marshal(OutputStream outputStream) throws IOException {
147: // Delegate to the marshal(Writer) method
148: marshal(new OutputStreamWriter(outputStream));
149: }
150:
151: public void marshal(Writer writer) throws IOException {
152: // Write out the XML declaration
153: writer.write("<?xml version=\"1.0\" ");
154: if (outputEncoding != null) {
155: writer.write("encoding=\"");
156: writer.write(outputEncoding);
157: writer.write("\"?>\n\n");
158:
159: } else {
160: writer.write("encoding=\"UTF-8\"?>\n\n");
161:
162: }
163: // Handle DOCTYPE declaration
164: writer.write(docTypeString);
165: writer.write("\n");
166: // Now start the recursive writing
167: writeXMLRepresentation(writer, "");
168:
169: // Close up
170: writer.flush();
171: writer.close();
172: }
173:
174: protected void writeXMLRepresentation(Writer writer, String indent)
175: throws IOException {
176:
177: writer.write(indent);
178: writer.write("<security-role");
179:
180: // Handle namespace mappings (if needed)
181: for (Iterator i = namespaceMappings.keySet().iterator(); i
182: .hasNext();) {
183: String prefix = (String) i.next();
184: String uri = (String) namespaceMappings.get(prefix);
185: writer.write(" xmlns");
186: if (!prefix.trim().equals("")) {
187: writer.write(":");
188: writer.write(prefix);
189: }
190: writer.write("=\"");
191: writer.write(uri);
192: writer.write("\"\n ");
193: }
194:
195: // Handle attributes (if needed)
196: if (zeus_IdSet) {
197: writer.write(" id=\"");
198: writer.write(escapeAttributeValue(id));
199: writer.write("\"");
200: }
201: writer.write(">");
202: writer.write("\n");
203:
204: // Handle child elements
205: if (description != null) {
206: ((DescriptionImpl) description).writeXMLRepresentation(
207: writer, new StringBuffer(indent).append(" ")
208: .toString());
209: }
210:
211: if (roleName != null) {
212: ((RoleNameImpl) roleName).writeXMLRepresentation(writer,
213: new StringBuffer(indent).append(" ").toString());
214: }
215:
216: writer.write(indent);
217: writer.write("</security-role>\n");
218: }
219:
220: private String escapeAttributeValue(String attributeValue) {
221: String returnValue = attributeValue;
222: for (int i = 0; i < returnValue.length(); i++) {
223: char ch = returnValue.charAt(i);
224: if (ch == '"') {
225: returnValue = new StringBuffer().append(
226: returnValue.substring(0, i)).append(""")
227: .append(returnValue.substring(i + 1))
228: .toString();
229: }
230: }
231: return returnValue;
232: }
233:
234: private String escapeTextValue(String textValue) {
235: String returnValue = textValue;
236: for (int i = 0; i < returnValue.length(); i++) {
237: char ch = returnValue.charAt(i);
238: if (ch == '<') {
239: returnValue = new StringBuffer().append(
240: returnValue.substring(0, i)).append("<")
241: .append(returnValue.substring(i + 1))
242: .toString();
243: } else if (ch == '>') {
244: returnValue = new StringBuffer().append(
245: returnValue.substring(0, i)).append(">")
246: .append(returnValue.substring(i + 1))
247: .toString();
248: }
249: }
250: return returnValue;
251: }
252:
253: /**
254: * <p>
255: * This sets a SAX <code>EntityResolver</code> for this unmarshalling process.
256: * </p>
257: *
258: * @param resolver the entity resolver to use.
259: */
260: public static void setEntityResolver(EntityResolver resolver) {
261: entityResolver = resolver;
262: }
263:
264: /**
265: * <p>
266: * This sets a SAX <code>ErrorHandler</code> for this unmarshalling process.
267: * </p>
268: *
269: * @param handler the entity resolver to use.
270: */
271: public static void setErrorHandler(ErrorHandler handler) {
272: errorHandler = handler;
273: }
274:
275: public static SecurityRole unmarshal(File file) throws IOException {
276: // Delegate to the unmarshal(Reader) method
277: return unmarshal(new FileReader(file));
278: }
279:
280: public static SecurityRole unmarshal(File file, boolean validate)
281: throws IOException {
282: // Delegate to the unmarshal(Reader) method
283: return unmarshal(new FileReader(file), validate);
284: }
285:
286: public static SecurityRole unmarshal(InputStream inputStream)
287: throws IOException {
288: // Delegate to the unmarshal(Reader) method
289: return unmarshal(new InputStreamReader(inputStream));
290: }
291:
292: public static SecurityRole unmarshal(InputStream inputStream,
293: boolean validate) throws IOException {
294: // Delegate to the unmarshal(Reader) method
295: return unmarshal(new InputStreamReader(inputStream), validate);
296: }
297:
298: public static SecurityRole unmarshal(Reader reader)
299: throws IOException {
300: // Delegate with default validation value
301: return unmarshal(reader, false);
302: }
303:
304: public static SecurityRole unmarshal(Reader reader, boolean validate)
305: throws IOException {
306: SecurityRoleImpl securityRole = SecurityRoleImpl.newInstance();
307: securityRole.setValidating(validate);
308: securityRole.setCurrentUNode(securityRole);
309: securityRole.setParentUNode(null);
310: // Load the XML parser
311: XMLReader parser = null;
312: String parserClass = System.getProperty("org.xml.sax.driver",
313: "org.apache.xerces.parsers.SAXParser");
314: try {
315: parser = XMLReaderFactory.createXMLReader(parserClass);
316:
317: // Set entity resolver, if needed
318: if (entityResolver != null) {
319: parser.setEntityResolver(entityResolver);
320: }
321:
322: // Set error handler
323: parser.setErrorHandler(securityRole);
324:
325: // Register lexical handler
326: parser.setProperty(
327: "http://xml.org/sax/properties/lexical-handler",
328: securityRole);
329:
330: // Register content handler
331: parser.setContentHandler(securityRole);
332: } catch (SAXException e) {
333: throw new IOException("Could not load XML parser: "
334: + e.getMessage());
335: }
336:
337: InputSource inputSource = new InputSource(reader);
338: try {
339: parser.setFeature("http://xml.org/sax/features/validation",
340: new Boolean(validate).booleanValue());
341: parser.setFeature("http://xml.org/sax/features/namespaces",
342: true);
343: parser.setFeature(
344: "http://xml.org/sax/features/namespace-prefixes",
345: false);
346: parser.parse(inputSource);
347: } catch (SAXException e) {
348: throw new IOException("Error parsing XML document: "
349: + e.getMessage());
350: }
351:
352: // Return the resultant object
353: return securityRole;
354: }
355:
356: public Unmarshallable getParentUNode() {
357: return zeus_parentUNode;
358: }
359:
360: public void setParentUNode(Unmarshallable parentUNode) {
361: this .zeus_parentUNode = parentUNode;
362: }
363:
364: public Unmarshallable getCurrentUNode() {
365: return zeus_currentUNode;
366: }
367:
368: public void setCurrentUNode(Unmarshallable currentUNode) {
369: this .zeus_currentUNode = currentUNode;
370: }
371:
372: public void setValidating(boolean validate) {
373: this .validate = validate;
374: }
375:
376: public void startDocument() throws SAXException {
377: // no-op
378: }
379:
380: public void setDocumentLocator(Locator locator) {
381: // no-op
382: }
383:
384: public void startPrefixMapping(String prefix, String uri)
385: throws SAXException {
386: namespaceMappings.put(prefix, uri);
387: }
388:
389: public void startElement(String namespaceURI, String localName,
390: String qName, org.xml.sax.Attributes atts)
391: throws SAXException {
392:
393: // Feed this to the correct ContentHandler
394: Unmarshallable current = getCurrentUNode();
395: if (current != this ) {
396: current.startElement(namespaceURI, localName, qName, atts);
397: return;
398: }
399:
400: // See if we handle, or we delegate
401: if ((localName.equals("security-role"))
402: && (!zeus_this NodeHandled)) {
403: // Handle ourselves
404: for (int i = 0, len = atts.getLength(); i < len; i++) {
405: String attName = atts.getLocalName(i);
406: String attValue = atts.getValue(i);
407: if (attName.equals("id")) {
408: setId(attValue);
409: }
410: }
411: zeus_this NodeHandled = true;
412: return;
413: } else {
414: // Delegate handling
415: if (localName.equals("description")
416: && (description == null)) {
417: DescriptionImpl description = DescriptionImpl
418: .newInstance();
419: current = getCurrentUNode();
420: description.setParentUNode(current);
421: description.setCurrentUNode(description);
422: this .setCurrentUNode(description);
423: description.startElement(namespaceURI, localName,
424: qName, atts);
425: // Add this value in
426: this .description = description;
427: return;
428: }
429: if (localName.equals("role-name") && (roleName == null)) {
430: RoleNameImpl roleName = RoleNameImpl.newInstance();
431: current = getCurrentUNode();
432: roleName.setParentUNode(current);
433: roleName.setCurrentUNode(roleName);
434: this .setCurrentUNode(roleName);
435: roleName.startElement(namespaceURI, localName, qName,
436: atts);
437: // Add this value in
438: this .roleName = roleName;
439: return;
440: }
441: }
442: }
443:
444: public void endElement(String namespaceURI, String localName,
445: String qName) throws SAXException {
446:
447: Unmarshallable current = getCurrentUNode();
448: if (current != this ) {
449: current.endElement(namespaceURI, localName, qName);
450: return;
451: }
452:
453: Unmarshallable parent = getCurrentUNode().getParentUNode();
454: if (parent != null) {
455: parent.setCurrentUNode(parent);
456: }
457: }
458:
459: public void characters(char[] ch, int start, int len)
460: throws SAXException {
461:
462: // Feed this to the correct ContentHandler
463: Unmarshallable current = getCurrentUNode();
464: if (current != this ) {
465: current.characters(ch, start, len);
466: return;
467: }
468:
469: String text = new String(ch, start, len);
470: }
471:
472: public void comment(char ch[], int start, int len)
473: throws SAXException {
474: // Currently no-op
475: }
476:
477: public void warning(SAXParseException e) throws SAXException {
478: if (errorHandler != null) {
479: errorHandler.warning(e);
480: }
481: }
482:
483: public void error(SAXParseException e) throws SAXException {
484: if ((validate) && (!hasDTD)) {
485: throw new SAXException(
486: "Validation is turned on, but no DTD has been specified in the input XML document. Please supply a DTD through a DOCTYPE reference.");
487: }
488: if (errorHandler != null) {
489: errorHandler.error(e);
490: }
491: }
492:
493: public void fatalError(SAXParseException e) throws SAXException {
494: if ((validate) && (!hasDTD)) {
495: throw new SAXException(
496: "Validation is turned on, but no DTD has been specified in the input XML document. Please supply a DTD through a DOCTYPE reference.");
497: }
498: if (errorHandler != null) {
499: errorHandler.fatalError(e);
500: }
501: }
502:
503: public void startDTD(String name, String publicID, String systemID)
504: throws SAXException {
505:
506: if ((name == null) || (name.equals(""))) {
507: docTypeString = "";
508: return;
509: }
510:
511: hasDTD = true;
512: StringBuffer docTypeSB = new StringBuffer();
513: boolean hasPublic = false;
514:
515: docTypeSB.append("<!DOCTYPE ").append(name);
516:
517: if ((publicID != null) && (!publicID.equals(""))) {
518: docTypeSB.append(" PUBLIC \"").append(publicID)
519: .append("\"");
520: hasPublic = true;
521: }
522:
523: if ((systemID != null) && (!systemID.equals(""))) {
524: if (!hasPublic) {
525: docTypeSB.append(" SYSTEM");
526: }
527: docTypeSB.append(" \"").append(systemID).append("\"");
528:
529: }
530:
531: docTypeSB.append(">");
532:
533: docTypeString = docTypeSB.toString();
534: }
535:
536: public void endDTD() throws SAXException {
537: // Currently no-op
538: }
539:
540: public void startEntity(String name) throws SAXException {
541: // Currently no-op
542: }
543:
544: public void endEntity(String name) throws SAXException {
545: // Currently no-op
546: }
547:
548: public void startCDATA() throws SAXException {
549: // Currently no-op
550: }
551:
552: public void endCDATA() throws SAXException {
553: // Currently no-op
554: }
555:
556: }
|