1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.jelly.tags.validate;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileNotFoundException;
22 import java.io.InputStream;
23 import java.io.IOException;
24
25 import org.apache.commons.jelly.JellyTagException;
26 import org.apache.commons.jelly.MissingAttributeException;
27 import org.apache.commons.jelly.TagSupport;
28 import org.apache.commons.jelly.XMLOutput;
29 import org.apache.commons.jelly.util.ClassLoaderUtils;
30 import org.iso_relax.verifier.Schema;
31 import org.iso_relax.verifier.Verifier;
32 import org.iso_relax.verifier.VerifierConfigurationException;
33 import org.iso_relax.verifier.VerifierFactory;
34 import org.xml.sax.SAXException;
35
36 /***
37 * This tag creates a new Verifier of a schema as a variable
38 * so that it can be used by a <validate> tag.
39 *
40 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
41 * @version $Revision: 155420 $
42 */
43 public class VerifierTag extends TagSupport {
44
45 /*** the variable name to export the Verifier as */
46 private String var;
47
48 /*** The URI to load the schema from */
49 private String uri;
50
51 /*** The file to load the schema from */
52 private File file;
53
54 /*** The system ID to use when parsing the schema */
55 private String systemId;
56
57 /*** The factory used to create new schema verifier objects */
58 private VerifierFactory factory;
59
60
61
62 public void doTag(final XMLOutput output) throws MissingAttributeException, JellyTagException {
63 if ( var == null ) {
64 throw new MissingAttributeException("var");
65 }
66
67 InputStream in = null;
68 if ( uri != null ) {
69 in = context.getResourceAsStream( uri );
70 if ( in == null ) {
71 throw new JellyTagException( "Could not find resource for uri: " + uri );
72 }
73 } else if (file != null) {
74 try {
75 in = new FileInputStream(file);
76 } catch (FileNotFoundException e) {
77 throw new JellyTagException(e);
78 }
79 } else {
80 String text = getBodyText();
81 in = new ByteArrayInputStream( text.getBytes() );
82 }
83
84 Verifier verifier = null;
85 try {
86 Schema schema = null;
87 if (systemId != null) {
88 schema = getFactory().compileSchema(in, systemId);
89 }
90 else if ( uri != null ) {
91 schema = getFactory().compileSchema(in, uri);
92 }
93 else{
94 schema = getFactory().compileSchema(in);
95 }
96
97 if ( schema == null ) {
98 throw new JellyTagException( "Could not create a valid schema" );
99 }
100
101 verifier = schema.newVerifier();
102 }
103 catch (VerifierConfigurationException e) {
104 throw new JellyTagException(e);
105 }
106 catch (SAXException e) {
107 throw new JellyTagException(e);
108 }
109 catch (IOException e) {
110 throw new JellyTagException(e);
111 }
112
113 context.setVariable(var, verifier);
114 }
115
116
117
118
119 /***
120 * Sets the name of the variable that will be set to the new Verifier
121 *
122 * @jelly:required
123 */
124 public void setVar(String var) {
125 this.var = var;
126 }
127
128 /***
129 * Sets the URI of the schema file to parse. If no URI and no file is
130 * specified then the body of this tag is used as the source of the schema
131 *
132 * @jelly:optional
133 */
134 public void setUri(String uri) {
135 this.uri = uri;
136 }
137
138 /***
139 * Sets the {@link File} of the schema to parse. If no URI and no file is
140 * specified then the body of this tag is used as the source of the schema
141 *
142 * @jelly:optional
143 */
144 public void setFile(File aFile) {
145 file = aFile;
146 }
147
148 /***
149 * Sets the system ID used when parsing the schema
150 *
151 * @jelly:optional
152 */
153 public void setSystemId(String systemId) {
154 this.systemId = systemId;
155 }
156
157 /***
158 * Sets the factory used to create new schema verifier objects.
159 * If none is provided then the default MSV factory is used.
160 *
161 * @jelly:optional
162 */
163 public void setFactory(VerifierFactory factory) {
164 this.factory = factory;
165 }
166
167 public VerifierFactory getFactory() throws JellyTagException {
168 if ( factory == null ) {
169 try {
170 ClassLoader loader = ClassLoaderUtils.getClassLoader(null, true, getClass());
171 factory = (VerifierFactory)loader.loadClass(
172 "com.sun.msv.verifier.jarv.TheFactoryImpl").newInstance();
173 } catch (ClassNotFoundException e) {
174 throw new JellyTagException(e);
175 } catch (InstantiationException e) {
176 throw new JellyTagException(e);
177 } catch (IllegalAccessException e) {
178 throw new JellyTagException(e);
179 }
180 }
181 return factory;
182 }
183
184
185
186
187
188 }