%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.commons.jelly.tags.validate.VerifierTag |
|
|
1 | /* |
|
2 | * Copyright 2002,2004 The Apache Software Foundation. |
|
3 | * |
|
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 | * you may not use this file except in compliance with the License. |
|
6 | * You may obtain a copy of the License at |
|
7 | * |
|
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 | * |
|
10 | * Unless required by applicable law or agreed to in writing, software |
|
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 | * See the License for the specific language governing permissions and |
|
14 | * limitations under the License. |
|
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: 1.10 $ |
|
42 | */ |
|
43 | 8 | 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 | // Tag interface |
|
61 | //------------------------------------------------------------------------- |
|
62 | public void doTag(final XMLOutput output) throws MissingAttributeException, JellyTagException { |
|
63 | 8 | if ( var == null ) { |
64 | 0 | throw new MissingAttributeException("var"); |
65 | } |
|
66 | ||
67 | 8 | InputStream in = null; |
68 | 8 | if ( uri != null ) { |
69 | 8 | in = context.getResourceAsStream( uri ); |
70 | 8 | if ( in == null ) { |
71 | 0 | throw new JellyTagException( "Could not find resource for uri: " + uri ); |
72 | } |
|
73 | 0 | } else if (file != null) { |
74 | try { |
|
75 | 0 | in = new FileInputStream(file); |
76 | 0 | } catch (FileNotFoundException e) { |
77 | 0 | throw new JellyTagException(e); |
78 | 0 | } |
79 | } else { |
|
80 | 0 | String text = getBodyText(); |
81 | 0 | in = new ByteArrayInputStream( text.getBytes() ); |
82 | } |
|
83 | ||
84 | 8 | Verifier verifier = null; |
85 | try { |
|
86 | 8 | Schema schema = null; |
87 | 8 | if (systemId != null) { |
88 | 0 | schema = getFactory().compileSchema(in, systemId); |
89 | } |
|
90 | 8 | else if ( uri != null ) { |
91 | 8 | schema = getFactory().compileSchema(in, uri); |
92 | } |
|
93 | else{ |
|
94 | 0 | schema = getFactory().compileSchema(in); |
95 | } |
|
96 | ||
97 | 8 | if ( schema == null ) { |
98 | 0 | throw new JellyTagException( "Could not create a valid schema" ); |
99 | } |
|
100 | ||
101 | 8 | verifier = schema.newVerifier(); |
102 | 8 | } |
103 | catch (VerifierConfigurationException e) { |
|
104 | 0 | throw new JellyTagException(e); |
105 | } |
|
106 | catch (SAXException e) { |
|
107 | 0 | throw new JellyTagException(e); |
108 | } |
|
109 | catch (IOException e) { |
|
110 | 0 | throw new JellyTagException(e); |
111 | } |
|
112 | ||
113 | 8 | context.setVariable(var, verifier); |
114 | 8 | } |
115 | ||
116 | // Properties |
|
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 | 8 | this.var = class="keyword">var; |
126 | 8 | } |
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 | 8 | this.uri = uri; |
136 | 8 | } |
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 | 0 | file = aFile; |
146 | 0 | } |
147 | ||
148 | /** |
|
149 | * Sets the system ID used when parsing the schema |
|
150 | * |
|
151 | * @jelly:optional |
|
152 | */ |
|
153 | public void setSystemId(String systemId) { |
|
154 | 0 | this.systemId = systemId; |
155 | 0 | } |
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 | 0 | this.factory = factory; |
165 | 0 | } |
166 | ||
167 | public VerifierFactory getFactory() throws JellyTagException { |
|
168 | 8 | if ( factory == null ) { |
169 | try { |
|
170 | 8 | ClassLoader loader = ClassLoaderUtils.getClassLoader(null, true, getClass()); |
171 | 8 | factory = (VerifierFactory)loader.loadClass( |
172 | "com.sun.msv.verifier.jarv.TheFactoryImpl").newInstance(); |
|
173 | 8 | } catch (ClassNotFoundException e) { |
174 | 0 | throw new JellyTagException(e); |
175 | } catch (InstantiationException e) { |
|
176 | 0 | throw new JellyTagException(e); |
177 | } catch (IllegalAccessException e) { |
|
178 | 0 | throw new JellyTagException(e); |
179 | } |
|
180 | } |
|
181 | 8 | return factory; |
182 | } |
|
183 | ||
184 | // Implementation methods |
|
185 | //------------------------------------------------------------------------- |
|
186 | ||
187 | ||
188 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |