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.core.impl;
17  
18  import java.io.File;
19  import java.io.FileFilter;
20  import java.io.IOException;
21  import java.net.URL;
22  import java.util.ArrayList;
23  import java.util.Enumeration;
24  import java.util.List;
25  import java.util.jar.JarEntry;
26  import java.util.jar.JarFile;
27  
28  import org.seasar.framework.container.annotation.tiger.Binding;
29  import org.seasar.framework.container.annotation.tiger.BindingType;
30  import org.seasar.framework.util.JarFileUtil;
31  import org.seasar.framework.util.StringUtil;
32  import org.seasar.uruma.core.TemplateManager;
33  import org.seasar.uruma.core.UrumaConstants;
34  import org.seasar.uruma.core.UrumaMessageCodes;
35  import org.seasar.uruma.core.ViewTemplateLoader;
36  import org.seasar.uruma.core.io.ExtFileFilter;
37  import org.seasar.uruma.log.UrumaLogger;
38  import org.seasar.uruma.rcp.util.RcpResourceUtil;
39  import org.seasar.uruma.util.PathUtil;
40  
41  /**
42   * {@link ViewTemplateLoader} の実装クラスです。<br />
43   * 
44   * @author y-komori
45   */
46  public class ViewTemplateLoaderImpl implements ViewTemplateLoader,
47          UrumaConstants, UrumaMessageCodes {
48      private static final UrumaLogger logger = UrumaLogger
49              .getLogger(ViewTemplateLoader.class);
50  
51      private TemplateManager templateManager;
52  
53      private static final FileFilter filter = new ExtFileFilter("xml");
54  
55      /*
56       * @see org.seasar.uruma.core.ViewTemplateLoader#loadViewTemplates()
57       */
58      public void loadViewTemplates(final URL resourceUrl) throws IOException {
59  
60          // TODO プロトコル毎にクラスを分けて整理する
61          if (UrumaConstants.PROTCOL_FILE.equals(resourceUrl.getProtocol())) {
62              File localFile = new File(resourceUrl.getPath());
63              File baseDir = new File(localFile.getParent() + SLASH
64                      + DEFAULT_VIEWS_PATH);
65  
66              logger.log(UrumaMessageCodes.FINDING_XML_START, baseDir
67                      .getAbsolutePath());
68  
69              List<File> pathList = RcpResourceUtil.findFileResources(baseDir,
70                      filter);
71  
72              String localBasePath = PathUtil.getParent(localFile
73                      .getAbsolutePath());
74              List<String> viewFilePaths = new ArrayList<String>(pathList.size());
75              for (File file : pathList) {
76                  String path = PathUtil.getRelativePath(localBasePath, file
77                          .getAbsolutePath());
78                  viewFilePaths.add(PathUtil.replaceSeparator(path));
79              }
80  
81              templateManager.loadTemplates(viewFilePaths);
82          } else if (PROTCOL_JAR.equals(resourceUrl.getProtocol())) {
83              String jarFilePath = StringUtil.substringFromLast(resourceUrl
84                      .getPath(), EXCLAMATION_MARK);
85              String jarLocalPath = StringUtil.substringToLast(
86                      resourceUrl.getPath(), EXCLAMATION_MARK).substring(1);
87              // workebnch.xml の親ディレクトリをクラスパスルートとみなす
88              String classPathRoot = PathUtil.getParent(jarLocalPath) + SLASH;
89              JarFile jarFile = JarFileUtil.create((new URL(jarFilePath))
90                      .getFile());
91  
92              logger.log(FINDING_XML_START, jarFilePath + EXCLAMATION_MARK
93                      + classPathRoot);
94  
95              // TODO SUSIE
96              String basePath = DEFAULT_VIEWS_PATH + SLASH;
97  
98              List<String> viewFilePaths = new ArrayList<String>();
99              Enumeration<JarEntry> entries = jarFile.entries();
100             while (entries.hasMoreElements()) {
101                 JarEntry entry = entries.nextElement();
102 
103                 String entryPath = entry.getName();
104                 if (entryPath.startsWith(basePath)
105                         && entryPath.endsWith(".xml")) {
106                     viewFilePaths.add(PathUtil.replaceSeparator(entryPath));
107                 }
108             }
109 
110             templateManager.loadTemplates(viewFilePaths);
111         }
112     }
113 
114     /**
115      * {@link TemplateManager} を設定します。<br />
116      * 
117      * @param templateManager
118      *      {@link TemplateManager}
119      */
120     @Binding(bindingType = BindingType.MUST)
121     public void setTemplateManager(final TemplateManager templateManager) {
122         this.templateManager = templateManager;
123     }
124 }