|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| package org.objectledge.pico.xml; |
|
29 |
| |
|
30 |
| import java.io.IOException; |
|
31 |
| import java.net.URL; |
|
32 |
| |
|
33 |
| import javax.xml.parsers.SAXParserFactory; |
|
34 |
| |
|
35 |
| import org.nanocontainer.integrationkit.PicoCompositionException; |
|
36 |
| import org.nanocontainer.script.NanoContainerMarkupException; |
|
37 |
| import org.nanocontainer.script.ScriptedContainerBuilder; |
|
38 |
| import org.objectledge.filesystem.FileSystem; |
|
39 |
| import org.objectledge.xml.XMLValidator; |
|
40 |
| import org.picocontainer.PicoContainer; |
|
41 |
| import org.xml.sax.EntityResolver; |
|
42 |
| import org.xml.sax.InputSource; |
|
43 |
| import org.xml.sax.SAXParseException; |
|
44 |
| import org.xml.sax.XMLReader; |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| public class LedgeContainerBuilder |
|
71 |
| extends ScriptedContainerBuilder |
|
72 |
| { |
|
73 |
| |
|
74 |
| public static final String SCHEMA_PATH = "org/objectledge/pico/xml/container.rng"; |
|
75 |
| |
|
76 |
| private final URL scriptURL; |
|
77 |
| private final ClassLoader classLoader; |
|
78 |
| private final SAXParserFactory parserFactory; |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
258
| public LedgeContainerBuilder(final URL configURL, final ClassLoader classLoader)
|
|
87 |
| { |
|
88 |
258
| super(configURL, classLoader);
|
|
89 |
258
| this.scriptURL = configURL;
|
|
90 |
258
| this.classLoader = classLoader;
|
|
91 |
258
| try
|
|
92 |
| { |
|
93 |
258
| parserFactory = SAXParserFactory.newInstance();
|
|
94 |
258
| parserFactory.setNamespaceAware(true);
|
|
95 |
| } |
|
96 |
| catch(Exception e) |
|
97 |
| { |
|
98 |
0
| throw new NanoContainerMarkupException(e);
|
|
99 |
| } |
|
100 |
| } |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
258
| protected PicoContainer createContainerFromScript(PicoContainer parentContainer,
|
|
106 |
| Object assemblyScope) |
|
107 |
| { |
|
108 |
258
| validate(scriptURL, parentContainer);
|
|
109 |
258
| CompositionContentHandler handler = new CompositionContentHandler(
|
|
110 |
| parentContainer, classLoader, assemblyScope); |
|
111 |
258
| try
|
|
112 |
| { |
|
113 |
258
| XMLReader reader = parserFactory.newSAXParser().getXMLReader();
|
|
114 |
258
| reader.setEntityResolver(new EntityResolver()
|
|
115 |
| { |
|
116 |
0
| public InputSource resolveEntity(String publicId, String systemId)
|
|
117 |
| throws IOException |
|
118 |
| { |
|
119 |
0
| URL url = new URL(scriptURL, systemId);
|
|
120 |
0
| return new InputSource(url.openStream());
|
|
121 |
| } |
|
122 |
| }); |
|
123 |
258
| reader.setContentHandler(handler);
|
|
124 |
258
| reader.parse(new InputSource(scriptURL.toString()));
|
|
125 |
258
| return handler.getResult();
|
|
126 |
| } |
|
127 |
| catch(SAXParseException e) |
|
128 |
| { |
|
129 |
0
| throw new PicoCompositionException("parse error " + e.getMessage() + " in " +
|
|
130 |
| e.getSystemId() + " at line " + e.getLineNumber(), e); |
|
131 |
| } |
|
132 |
| catch(Exception e) |
|
133 |
| { |
|
134 |
0
| throw new PicoCompositionException("error processing " + scriptURL, e);
|
|
135 |
| } |
|
136 |
| } |
|
137 |
| |
|
138 |
258
| private void validate(URL scriptURL, PicoContainer container)
|
|
139 |
| { |
|
140 |
258
| XMLValidator validator = (XMLValidator)container.getComponentInstance(XMLValidator.class);
|
|
141 |
258
| FileSystem fileSystem = (FileSystem)container.getComponentInstance(FileSystem.class);
|
|
142 |
| |
|
143 |
258
| try
|
|
144 |
| { |
|
145 |
258
| validator.validate(scriptURL, fileSystem.getResource(SCHEMA_PATH));
|
|
146 |
| } |
|
147 |
| catch(SAXParseException e) |
|
148 |
| { |
|
149 |
0
| throw new PicoCompositionException("parse error " + e.getMessage() + " in " +
|
|
150 |
| e.getSystemId() + " at line " + e.getLineNumber(), e); |
|
151 |
| } |
|
152 |
| catch(Exception e) |
|
153 |
| { |
|
154 |
0
| throw new PicoCompositionException("composition file " + scriptURL +
|
|
155 |
| " is missing or invalid", e); |
|
156 |
| } |
|
157 |
| } |
|
158 |
| } |