1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.isenshi.util.extlib;
17
18 import java.io.File;
19 import java.io.OutputStream;
20 import java.io.OutputStreamWriter;
21 import java.io.PrintWriter;
22 import java.io.UnsupportedEncodingException;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.LinkedHashMap;
28 import java.util.List;
29 import java.util.Map;
30
31 import org.seasar.framework.container.ComponentDef;
32 import org.seasar.framework.container.S2Container;
33 import org.seasar.framework.container.factory.S2ContainerFactory;
34 import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
35
36 import com.isenshi.util.DiconStringBuffer;
37 import com.isenshi.util.ResourceUtils;
38
39 public class DiconResource {
40
41 private static final String ENCODING = "Windows-31J";
42
43 public static final String OPTION_NAME = "name";
44
45 public static final String OPTION_ASPECTS = "option.ascpects";
46
47 private Map componentMap_ = new LinkedHashMap();
48
49 private List refComponentList_ = new ArrayList();
50
51 private String path_;
52
53 private S2Container container;
54
55 private String namespace;
56
57 private Map propertiesMap = new HashMap();
58
59 public DiconResource() {
60 }
61
62 public boolean hasContainer() {
63 return container != null;
64 }
65
66 public void setPath(String path) {
67 this.path_ = path;
68 }
69
70 public void addComponent(Object component) {
71 addComponent(component, null);
72 }
73
74 public void addComponent(Object component, Map options) {
75 componentMap_.put(component, options);
76 }
77
78 public void addComponent(Object component, Map options, String[] properties) {
79 componentMap_.put(component, options);
80 propertiesMap.put(component, properties);
81 }
82
83 public void addComponentAll(Collection col) {
84 addComponentAll(col, null);
85 }
86
87 public void addComponentAll(Collection col, Map options) {
88 for (Iterator i = col.iterator(); i.hasNext();) {
89 addComponent(i.next(), options);
90 }
91 }
92
93 public void load() {
94 File file = ResourceUtils.getFile(path_);
95 if (!file.exists()) {
96 return;
97 }
98 container = S2ContainerFactory.create(path_);
99 }
100
101 public void bindRoot() {
102 if (SingletonS2ContainerFactory.hasContainer()) {
103 S2Container rootContainer = SingletonS2ContainerFactory
104 .getContainer();
105 container.setRoot(rootContainer);
106 rootContainer.include(container);
107 }
108 }
109
110 public S2Container getContainer() {
111 return container;
112 }
113
114 public void include(String path) {
115 S2Container rootContainer = SingletonS2ContainerFactory.getContainer();
116 S2Container includeContainer = rootContainer.getDescendant(path);
117 container.include(includeContainer);
118 }
119
120 public Object[] findComponents(Object componentKey) {
121 return container.findComponents(componentKey);
122 }
123
124 public ComponentDef[] findComponentDefs(Object componentKey){
125 return container.findComponentDefs(componentKey);
126 }
127
128 public Object getComponent(Object componentKey) {
129 return container.getComponent(componentKey);
130 }
131
132 public void save() {
133 OutputStream stream = ResourceUtils.getOutputStream(path_);
134 save(stream, null);
135 }
136
137 public void save(OutputStream stream) {
138 save(stream, null);
139 }
140
141 public void addRefComponent(Object refComponent) {
142 refComponentList_.add(refComponent);
143 }
144
145 public void setNamespace(String namespace) {
146 this.namespace = namespace;
147 }
148
149 public String getNamespace() {
150 if (hasContainer()) {
151 return container.getNamespace();
152 } else {
153 return namespace;
154 }
155 }
156
157 public void save(OutputStream stream, Map options) {
158 DiconStringBuffer buf = new DiconStringBuffer();
159 buf.appendStartComponentsTag();
160
161 if (namespace != null && namespace.length() > 0) {
162 buf.appendAttribute("namespace", namespace);
163 }
164
165 buf.setReferenceComponents(refComponentList_);
166
167 Iterator itr = componentMap_.keySet().iterator();
168 while (itr.hasNext()) {
169 Object component = itr.next();
170 Map componentOptions = (Map) componentMap_.get(component);
171 String[] properties = (String[]) propertiesMap.get(component);
172 if (properties != null) {
173 buf.appendComponent(component, componentOptions, properties);
174 } else {
175 buf.appendComponent(component, componentOptions);
176 }
177 }
178 buf.endComponentsTag();
179
180 PrintWriter writer = null;
181 try {
182 writer = new PrintWriter(new OutputStreamWriter(stream, ENCODING));
183 } catch (UnsupportedEncodingException e) {
184 e.printStackTrace();
185 throw new RuntimeException(e);
186 }
187 writer.write(buf.toString());
188 writer.close();
189 }
190 }