Coverage Report - org.seasar.uruma.component.factory.ComponentTreeBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
ComponentTreeBuilder
91%
29/32
50%
1/2
2
 
 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  148
 public class ComponentTreeBuilder {
 46  
 
 47  148
     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  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  
 }