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 | 16 | public class ViewTemplateLoaderImpl implements ViewTemplateLoader, |
47 | |
UrumaConstants, UrumaMessageCodes { |
48 | 4 | private static final UrumaLogger logger = UrumaLogger |
49 | |
.getLogger(ViewTemplateLoader.class); |
50 | |
|
51 | |
private TemplateManager templateManager; |
52 | |
|
53 | 4 | private static final FileFilter filter = new ExtFileFilter("xml"); |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
public void loadViewTemplates(final URL resourceUrl) throws IOException { |
59 | |
|
60 | |
|
61 | 0 | if (UrumaConstants.PROTCOL_FILE.equals(resourceUrl.getProtocol())) { |
62 | 0 | File localFile = new File(resourceUrl.getPath()); |
63 | 0 | File baseDir = new File(localFile.getParent() + SLASH |
64 | |
+ DEFAULT_VIEWS_PATH); |
65 | |
|
66 | 0 | logger.log(UrumaMessageCodes.FINDING_XML_START, baseDir |
67 | |
.getAbsolutePath()); |
68 | |
|
69 | 0 | List<File> pathList = RcpResourceUtil.findFileResources(baseDir, |
70 | |
filter); |
71 | |
|
72 | 0 | String localBasePath = PathUtil.getParent(localFile |
73 | |
.getAbsolutePath()); |
74 | 0 | List<String> viewFilePaths = new ArrayList<String>(pathList.size()); |
75 | 0 | for (File file : pathList) { |
76 | 0 | String path = PathUtil.getRelativePath(localBasePath, file |
77 | |
.getAbsolutePath()); |
78 | 0 | viewFilePaths.add(PathUtil.replaceSeparator(path)); |
79 | 0 | } |
80 | |
|
81 | 0 | templateManager.loadTemplates(viewFilePaths); |
82 | 0 | } else if (PROTCOL_JAR.equals(resourceUrl.getProtocol())) { |
83 | 0 | String jarFilePath = StringUtil.substringFromLast(resourceUrl |
84 | |
.getPath(), EXCLAMATION_MARK); |
85 | 0 | String jarLocalPath = StringUtil.substringToLast( |
86 | |
resourceUrl.getPath(), EXCLAMATION_MARK).substring(1); |
87 | |
|
88 | 0 | String classPathRoot = PathUtil.getParent(jarLocalPath) + SLASH; |
89 | 0 | JarFile jarFile = JarFileUtil.create((new URL(jarFilePath)) |
90 | |
.getFile()); |
91 | |
|
92 | 0 | logger.log(FINDING_XML_START, jarFilePath + EXCLAMATION_MARK |
93 | |
+ classPathRoot); |
94 | |
|
95 | |
|
96 | 0 | String basePath = DEFAULT_VIEWS_PATH + SLASH; |
97 | |
|
98 | 0 | List<String> viewFilePaths = new ArrayList<String>(); |
99 | 0 | Enumeration<JarEntry> entries = jarFile.entries(); |
100 | 0 | while (entries.hasMoreElements()) { |
101 | 0 | JarEntry entry = entries.nextElement(); |
102 | |
|
103 | 0 | String entryPath = entry.getName(); |
104 | 0 | if (entryPath.startsWith(basePath) |
105 | |
&& entryPath.endsWith(".xml")) { |
106 | 0 | viewFilePaths.add(PathUtil.replaceSeparator(entryPath)); |
107 | |
} |
108 | 0 | } |
109 | |
|
110 | 0 | templateManager.loadTemplates(viewFilePaths); |
111 | |
} |
112 | 0 | } |
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
@Binding(bindingType = BindingType.MUST) |
121 | |
public void setTemplateManager(final TemplateManager templateManager) { |
122 | 16 | this.templateManager = templateManager; |
123 | 16 | } |
124 | |
} |