1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
43
44
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
57
58 public void loadViewTemplates(final URL resourceUrl) throws IOException {
59
60
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
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
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
116
117
118
119
120 @Binding(bindingType = BindingType.MUST)
121 public void setTemplateManager(final TemplateManager templateManager) {
122 this.templateManager = templateManager;
123 }
124 }