1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.uruma.component.factory; |
17 | |
|
18 | |
import java.io.File; |
19 | |
import java.io.InputStream; |
20 | |
|
21 | |
import javax.xml.XMLConstants; |
22 | |
import javax.xml.parsers.SAXParser; |
23 | |
import javax.xml.parsers.SAXParserFactory; |
24 | |
import javax.xml.transform.stream.StreamSource; |
25 | |
import javax.xml.validation.Schema; |
26 | |
import javax.xml.validation.SchemaFactory; |
27 | |
|
28 | |
import org.seasar.framework.container.factory.ClassPathResourceResolver; |
29 | |
import org.seasar.framework.exception.ResourceNotFoundRuntimeException; |
30 | |
import org.seasar.framework.exception.SAXRuntimeException; |
31 | |
import org.seasar.framework.util.InputStreamUtil; |
32 | |
import org.seasar.framework.util.SAXParserFactoryUtil; |
33 | |
import org.seasar.framework.xml.SaxHandler; |
34 | |
import org.seasar.framework.xml.SaxHandlerParser; |
35 | |
import org.seasar.framework.xml.TagHandlerContext; |
36 | |
import org.seasar.uruma.component.Template; |
37 | |
import org.seasar.uruma.core.UrumaConstants; |
38 | |
import org.xml.sax.SAXException; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 148 | public class ComponentTreeBuilder { |
46 | |
|
47 | 148 | protected ClassPathResourceResolver resolver = new ClassPathResourceResolver(); |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
public Template build(final String path) { |
57 | 116 | final SaxHandlerParser parser = createSaxHandlerParser(path); |
58 | 116 | final InputStream is = getInputStream(path); |
59 | |
try { |
60 | 116 | Template template = (Template) parser.parse(is, path); |
61 | 116 | return template; |
62 | |
} finally { |
63 | 116 | InputStreamUtil.close(is); |
64 | |
} |
65 | |
} |
66 | |
|
67 | |
protected InputStream getInputStream(final String path) { |
68 | 232 | final InputStream is = resolver.getInputStream(path); |
69 | |
|
70 | 232 | if (is == null) { |
71 | 0 | throw new ResourceNotFoundRuntimeException(path); |
72 | |
} |
73 | 232 | return is; |
74 | |
} |
75 | |
|
76 | |
protected SaxHandlerParser createSaxHandlerParser(final String path) { |
77 | 116 | System.setProperty("javax.xml.parsers.SAXParserFactory", |
78 | |
"com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl"); |
79 | 116 | final SAXParserFactory factory = SAXParserFactoryUtil.newInstance(); |
80 | 116 | factory.setNamespaceAware(true); |
81 | |
|
82 | 116 | final SchemaFactory schemaFactory = SchemaFactory |
83 | |
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); |
84 | 116 | InputStream is = getInputStream(UrumaConstants.SCHEMA_PATH); |
85 | |
try { |
86 | 116 | final Schema schema = schemaFactory.newSchema(new StreamSource(is)); |
87 | 116 | factory.setSchema(schema); |
88 | 0 | } catch (SAXException ex) { |
89 | 0 | throw new SAXRuntimeException(ex); |
90 | |
} finally { |
91 | 116 | InputStreamUtil.close(is); |
92 | 116 | } |
93 | |
|
94 | 116 | final SAXParser saxParser = SAXParserFactoryUtil.newSAXParser(factory); |
95 | 116 | final SaxHandler handler = createSaxHandler(); |
96 | |
|
97 | 116 | createContext(handler, path); |
98 | |
|
99 | 116 | return new SaxHandlerParser(handler, saxParser); |
100 | |
} |
101 | |
|
102 | |
protected SaxHandler createSaxHandler() { |
103 | 116 | SaxHandler handler = new SaxHandler(new UrumaTagHandlerRule()); |
104 | 116 | return handler; |
105 | |
} |
106 | |
|
107 | |
protected void createContext(final SaxHandler handler, final String path) { |
108 | 116 | final TagHandlerContext context = handler.getTagHandlerContext(); |
109 | 116 | context.addParameter(UrumaTagHandler.PARAM_PATH, path); |
110 | 116 | context.addParameter(UrumaTagHandler.PARAM_BASE_PATH, (new File(path)) |
111 | |
.getParent()); |
112 | 116 | } |
113 | |
} |