View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
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, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
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   * 画面定義 XML ファイルを読み込み、コンポーネントツリーを生成するためのクラスです。<br />
42   * 
43   * @author y-komori
44   */
45  public class ComponentTreeBuilder {
46  
47      protected ClassPathResourceResolver resolver = new ClassPathResourceResolver();
48  
49      /**
50       * 指定されたパスの画面定義XMLを読み込み、コンポーネントツリーを生成します。<br />
51       * 
52       * @param path
53       *            画面定義XMLのパス
54       * @return {@link Template} オブジェクト
55       */
56      public Template build(final String path) {
57          final SaxHandlerParser parser = createSaxHandlerParser(path);
58          final InputStream is = getInputStream(path);
59          try {
60              Template template = (Template) parser.parse(is, path);
61              return template;
62          } finally {
63              InputStreamUtil.close(is);
64          }
65      }
66  
67      protected InputStream getInputStream(final String path) {
68          final InputStream is = resolver.getInputStream(path);
69  
70          if (is == null) {
71              throw new ResourceNotFoundRuntimeException(path);
72          }
73          return is;
74      }
75  
76      protected SaxHandlerParser createSaxHandlerParser(final String path) {
77          System.setProperty("javax.xml.parsers.SAXParserFactory",
78                  "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl");
79          final SAXParserFactory factory = SAXParserFactoryUtil.newInstance();
80          factory.setNamespaceAware(true);
81  
82          final SchemaFactory schemaFactory = SchemaFactory
83                  .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
84          InputStream is = getInputStream(UrumaConstants.SCHEMA_PATH);
85          try {
86              final Schema schema = schemaFactory.newSchema(new StreamSource(is));
87              factory.setSchema(schema);
88          } catch (SAXException ex) {
89              throw new SAXRuntimeException(ex);
90          } finally {
91              InputStreamUtil.close(is);
92          }
93  
94          final SAXParser saxParser = SAXParserFactoryUtil.newSAXParser(factory);
95          final SaxHandler handler = createSaxHandler();
96  
97          createContext(handler, path);
98  
99          return new SaxHandlerParser(handler, saxParser);
100     }
101 
102     protected SaxHandler createSaxHandler() {
103         SaxHandler handler = new SaxHandler(new UrumaTagHandlerRule());
104         return handler;
105     }
106 
107     protected void createContext(final SaxHandler handler, final String path) {
108         final TagHandlerContext context = handler.getTagHandlerContext();
109         context.addParameter(UrumaTagHandler.PARAM_PATH, path);
110         context.addParameter(UrumaTagHandler.PARAM_BASE_PATH, (new File(path))
111                 .getParent());
112     }
113 }