1 /*
2 * Copyright (c) 2003, Henri Yandell
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or
6 * without modification, are permitted provided that the
7 * following conditions are met:
8 *
9 * + Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * + Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * + Neither the name of XmlWriter nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32 package com.generationjava.io.xml;
33
34 import java.io.IOException;
35 import org.iso_relax.verifier.*;
36 import org.xml.sax.SAXException;
37 import org.xml.sax.SAXParseException;
38 import org.xml.sax.ErrorHandler;
39 import org.xml.sax.helpers.AttributesImpl;
40
41 /// TODO: Supply an ErrorHandler in a constructor
42 /***
43 * Validates the written XML against a DTD or XML Schema using
44 * the iso_relax/jarv library.
45 */
46 public class JarvWriter extends DelegatingXmlWriter implements ErrorHandler {
47
48 private VerifierHandler verifierHandler;
49 private String tag;
50 private AttributesImpl attrs = new AttributesImpl();
51 private String text;
52
53 public JarvWriter(XmlWriter writer, VerifierHandler verifierHandler) throws IOException {
54 super(writer);
55 this.verifierHandler = verifierHandler;
56 try {
57 verifierHandler.startDocument();
58 } catch(SAXException se) {
59 throw new IOException("Need to wrap the SAX exception in start document. ");
60 }
61 // try {
62 // VerifierFactory vf = new com.sun.msv.verifier.jarv.TheFactoryImpl();
63 //// VerifierFactory vf = new org.kohsuke.jarv.xerces.XercesVerifierFactory();
64 // Schema schema = vf.compileSchema("http://lleu.genscape.com/plantfeed.xsd");
65 // Verifier v = schema.newVerifier();
66 // v.setErrorHandler(this);
67 // VerifierHandler vh = v.getVerifierHandler();
68 }
69
70 /// TODO: Fix this
71 private String getDefaultNamespace() {
72 return "";
73 }
74
75 /// TODO: Change exception type
76 private void checkSchema() throws IOException {
77 if(this.tag == null) {
78 return;
79 }
80 // should use this.namespace
81 try {
82 verifierHandler.startElement( getDefaultNamespace(), this.tag, this.tag, attrs );
83 if(this.text != null) {
84 verifierHandler.characters( this.text.toCharArray(), 0, this.text.length() );
85 }
86 verifierHandler.endElement( getDefaultNamespace(), this.tag, this.tag);
87 } catch(SAXException se) {
88 throw new IOException("Need to wrap the SAX exception in start element: "+se);
89 }
90 this.attrs.clear();
91 this.tag = null;
92 this.text = null;
93 }
94
95 public XmlWriter writeEntity(String name) throws IOException {
96 this.tag = name;
97 return super.writeEntity(name);
98 }
99
100 public XmlWriter endEntity() throws IOException {
101 checkSchema();
102 return super.endEntity();
103 }
104
105 public XmlWriter writeAttribute(String attr, Object value) throws IOException {
106 if(value != null) {
107 // this.attrs.addAttribute( getDefaultNamespace(), attr, attr, value.getClass().getName(), toString(value) );
108 this.attrs.addAttribute( getDefaultNamespace(), attr, attr, value.getClass().getName(), value.toString() );
109 }
110 return super.writeAttribute(attr, value);
111 }
112
113 public XmlWriter writeText(Object text) throws IOException {
114 this.text = ""+text;
115 return super.writeText(text);
116 }
117
118 public void error(SAXParseException spe) {
119 System.err.println("Error: "+spe);
120 }
121 public void fatalError(SAXParseException spe) {
122 System.err.println("Fatal: "+spe);
123 }
124 public void warning(SAXParseException spe) {
125 System.err.println("Warning: "+spe);
126 }
127
128 public void close() throws IOException {
129 try {
130 verifierHandler.endDocument();
131 super.close();
132 } catch(SAXException se) {
133 throw new IOException("Need to wrap the SAX exception in end document. ");
134 }
135 }
136
137 }