Mercurial > projects > dwt-addons
annotate dwtx/jface/text/templates/persistence/TemplateReaderWriter.d @ 155:8442b6b2da2d
Fix import for ResourceBundle
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 25 Aug 2008 00:45:19 +0200 |
parents | 77b70cbb4c8d |
children | 1a5b8f8129df |
rev | line source |
---|---|
129 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2008 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwtx.jface.text.templates.persistence.TemplateReaderWriter; | |
14 | |
131 | 15 import dwtx.jface.text.templates.persistence.TemplatePersistenceData; // packageimport |
16 import dwtx.jface.text.templates.persistence.TemplatePersistenceMessages; // packageimport | |
17 import dwtx.jface.text.templates.persistence.TemplateStore; // packageimport | |
18 | |
19 | |
129 | 20 import dwt.dwthelper.utils; |
21 | |
22 import java.io.IOException; | |
23 import java.io.InputStream; | |
24 import java.io.OutputStream; | |
25 import java.io.Reader; | |
26 import java.io.Writer; | |
153
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
148
diff
changeset
|
27 import dwtx.dwtxhelper.Collection; |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
148
diff
changeset
|
28 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
148
diff
changeset
|
29 |
155
8442b6b2da2d
Fix import for ResourceBundle
Frank Benoit <benoit@tionex.de>
parents:
154
diff
changeset
|
30 import dwt.dwthelper.ResourceBundle; |
153
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
148
diff
changeset
|
31 |
129 | 32 |
33 import javax.xml.parsers.DocumentBuilder; | |
34 import javax.xml.parsers.DocumentBuilderFactory; | |
35 import javax.xml.parsers.ParserConfigurationException; | |
36 import javax.xml.transform.OutputKeys; | |
37 import javax.xml.transform.Transformer; | |
38 import javax.xml.transform.TransformerException; | |
39 import javax.xml.transform.TransformerFactory; | |
40 import javax.xml.transform.dom.DOMSource; | |
41 import javax.xml.transform.stream.StreamResult; | |
42 | |
43 import org.w3c.dom.Attr; | |
44 import org.w3c.dom.Document; | |
45 import org.w3c.dom.NamedNodeMap; | |
46 import org.w3c.dom.Node; | |
47 import org.w3c.dom.NodeList; | |
48 import org.w3c.dom.Text; | |
49 import org.xml.sax.InputSource; | |
50 import org.xml.sax.SAXException; | |
51 | |
52 import dwtx.core.runtime.Assert; | |
53 import dwtx.jface.text.templates.Template; | |
54 | |
55 /** | |
56 * Serializes templates as character or byte stream and reads the same format | |
57 * back. | |
58 * <p> | |
59 * Clients may instantiate this class, it is not intended to be | |
60 * subclassed.</p> | |
61 * | |
62 * @since 3.0 | |
63 * @noextend This class is not intended to be subclassed by clients. | |
64 */ | |
65 public class TemplateReaderWriter { | |
66 | |
145 | 67 private static const String TEMPLATE_ROOT = "templates"; //$NON-NLS-1$ |
68 private static const String TEMPLATE_ELEMENT = "template"; //$NON-NLS-1$ | |
69 private static const String NAME_ATTRIBUTE= "name"; //$NON-NLS-1$ | |
70 private static const String ID_ATTRIBUTE= "id"; //$NON-NLS-1$ | |
71 private static const String DESCRIPTION_ATTRIBUTE= "description"; //$NON-NLS-1$ | |
72 private static const String CONTEXT_ATTRIBUTE= "context"; //$NON-NLS-1$ | |
73 private static const String ENABLED_ATTRIBUTE= "enabled"; //$NON-NLS-1$ | |
74 private static const String DELETED_ATTRIBUTE= "deleted"; //$NON-NLS-1$ | |
129 | 75 /** |
76 * @since 3.1 | |
77 */ | |
145 | 78 private static const String AUTO_INSERTABLE_ATTRIBUTE= "autoinsert"; //$NON-NLS-1$ |
129 | 79 |
80 /** | |
81 * Create a new instance. | |
82 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
83 public this() { |
129 | 84 } |
85 | |
86 /** | |
87 * Reads templates from a reader and returns them. The reader must present | |
88 * a serialized form as produced by the <code>save</code> method. | |
89 * | |
90 * @param reader the reader to read templates from | |
91 * @return the read templates, encapsulated in instances of <code>TemplatePersistenceData</code> | |
92 * @throws IOException if reading from the stream fails | |
93 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
94 public TemplatePersistenceData[] read(Reader reader) { |
129 | 95 return read(reader, null); |
96 } | |
97 | |
98 /** | |
99 * Reads the template with identifier <code>id</code> from a reader and | |
100 * returns it. The reader must present a serialized form as produced by the | |
101 * <code>save</code> method. | |
102 * | |
103 * @param reader the reader to read templates from | |
104 * @param id the id of the template to return | |
105 * @return the read template, encapsulated in an instances of | |
106 * <code>TemplatePersistenceData</code> | |
107 * @throws IOException if reading from the stream fails | |
108 * @since 3.1 | |
109 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
110 public TemplatePersistenceData readSingle(Reader reader, String id) { |
129 | 111 TemplatePersistenceData[] datas= read(new InputSource(reader), null, id); |
112 if (datas.length > 0) | |
113 return datas[0]; | |
114 return null; | |
115 } | |
116 | |
117 /** | |
118 * Reads templates from a stream and adds them to the templates. | |
119 * | |
120 * @param reader the reader to read templates from | |
121 * @param bundle a resource bundle to use for translating the read templates, or <code>null</code> if no translation should occur | |
122 * @return the read templates, encapsulated in instances of <code>TemplatePersistenceData</code> | |
123 * @throws IOException if reading from the stream fails | |
124 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
125 public TemplatePersistenceData[] read(Reader reader, ResourceBundle bundle) { |
129 | 126 return read(new InputSource(reader), bundle, null); |
127 } | |
128 | |
129 /** | |
130 * Reads templates from a stream and adds them to the templates. | |
131 * | |
132 * @param stream the byte stream to read templates from | |
133 * @param bundle a resource bundle to use for translating the read templates, or <code>null</code> if no translation should occur | |
134 * @return the read templates, encapsulated in instances of <code>TemplatePersistenceData</code> | |
135 * @throws IOException if reading from the stream fails | |
136 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
137 public TemplatePersistenceData[] read(InputStream stream, ResourceBundle bundle) { |
129 | 138 return read(new InputSource(stream), bundle, null); |
139 } | |
140 | |
141 /** | |
142 * Reads templates from an <code>InputSource</code> and adds them to the templates. | |
143 * | |
144 * @param source the input source | |
145 * @param bundle a resource bundle to use for translating the read templates, or <code>null</code> if no translation should occur | |
146 * @param singleId the template id to extract, or <code>null</code> to read in all templates | |
147 * @return the read templates, encapsulated in instances of <code>TemplatePersistenceData</code> | |
148 * @throws IOException if reading from the stream fails | |
149 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
150 private TemplatePersistenceData[] read(InputSource source, ResourceBundle bundle, String singleId) { |
129 | 151 try { |
152 Collection templates= new ArrayList(); | |
153 Set ids= new HashSet(); | |
154 | |
155 DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance(); | |
156 DocumentBuilder parser= factory.newDocumentBuilder(); | |
157 Document document= parser.parse(source); | |
158 | |
159 NodeList elements= document.getElementsByTagName(TEMPLATE_ELEMENT); | |
160 | |
161 int count= elements.getLength(); | |
162 for (int i= 0; i !is count; i++) { | |
163 Node node= elements.item(i); | |
164 NamedNodeMap attributes= node.getAttributes(); | |
165 | |
166 if (attributes is null) | |
167 continue; | |
168 | |
169 String id= getStringValue(attributes, ID_ATTRIBUTE, null); | |
170 if (id !is null && ids.contains(id)) | |
171 throw new IOException(TemplatePersistenceMessages.getString("TemplateReaderWriter.duplicate.id")); //$NON-NLS-1$ | |
172 | |
173 if (singleId !is null && !singleId.equals(id)) | |
174 continue; | |
175 | |
176 bool deleted = getBooleanValue(attributes, DELETED_ATTRIBUTE, false); | |
177 | |
178 String name= getStringValue(attributes, NAME_ATTRIBUTE); | |
179 name= translateString(name, bundle); | |
180 | |
181 String description= getStringValue(attributes, DESCRIPTION_ATTRIBUTE, ""); //$NON-NLS-1$ | |
182 description= translateString(description, bundle); | |
183 | |
184 String context= getStringValue(attributes, CONTEXT_ATTRIBUTE); | |
185 | |
186 if (name is null || context is null) | |
187 throw new IOException(TemplatePersistenceMessages.getString("TemplateReaderWriter.error.missing_attribute")); //$NON-NLS-1$ | |
188 | |
189 bool enabled = getBooleanValue(attributes, ENABLED_ATTRIBUTE, true); | |
190 bool autoInsertable= getBooleanValue(attributes, AUTO_INSERTABLE_ATTRIBUTE, true); | |
191 | |
192 StringBuffer buffer= new StringBuffer(); | |
193 NodeList children= node.getChildNodes(); | |
194 for (int j= 0; j !is children.getLength(); j++) { | |
195 String value= children.item(j).getNodeValue(); | |
196 if (value !is null) | |
197 buffer.append(value); | |
198 } | |
199 String pattern= buffer.toString(); | |
200 pattern= translateString(pattern, bundle); | |
201 | |
145 | 202 Template template_= new Template(name, description, context, pattern, autoInsertable); |
203 TemplatePersistenceData data= new TemplatePersistenceData(template_, enabled, id); | |
129 | 204 data.setDeleted(deleted); |
205 | |
206 templates.add(data); | |
207 | |
208 if (singleId !is null && singleId.equals(id)) | |
209 break; | |
210 } | |
211 | |
212 return (TemplatePersistenceData[]) templates.toArray(new TemplatePersistenceData[templates.size()]); | |
213 | |
214 } catch (ParserConfigurationException e) { | |
215 Assert.isTrue(false); | |
216 } catch (SAXException e) { | |
217 Throwable t= e.getCause(); | |
138 | 218 if ( cast(IOException)t ) |
134 | 219 throw cast(IOException) t; |
129 | 220 else if (t !is null) |
221 throw new IOException(t.getMessage()); | |
222 else | |
223 throw new IOException(e.getMessage()); | |
224 } | |
225 | |
226 return null; // dummy | |
227 } | |
228 | |
229 /** | |
230 * Saves the templates as XML, encoded as UTF-8 onto the given byte stream. | |
231 * | |
232 * @param templates the templates to save | |
233 * @param stream the byte output to write the templates to in XML | |
234 * @throws IOException if writing the templates fails | |
235 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
236 public void save(TemplatePersistenceData[] templates, OutputStream stream) { |
129 | 237 save(templates, new StreamResult(stream)); |
238 } | |
239 | |
240 /** | |
241 * Saves the templates as XML. | |
242 * | |
243 * @param templates the templates to save | |
244 * @param writer the writer to write the templates to in XML | |
245 * @throws IOException if writing the templates fails | |
246 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
247 public void save(TemplatePersistenceData[] templates, Writer writer) { |
129 | 248 save(templates, new StreamResult(writer)); |
249 } | |
250 | |
251 /** | |
252 * Saves the templates as XML. | |
253 * | |
254 * @param templates the templates to save | |
255 * @param result the stream result to write to | |
256 * @throws IOException if writing the templates fails | |
257 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
258 private void save(TemplatePersistenceData[] templates, StreamResult result) { |
129 | 259 try { |
260 DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance(); | |
261 DocumentBuilder builder= factory.newDocumentBuilder(); | |
262 Document document= builder.newDocument(); | |
263 | |
264 Node root= document.createElement(TEMPLATE_ROOT); | |
265 document.appendChild(root); | |
266 | |
267 for (int i= 0; i < templates.length; i++) { | |
268 TemplatePersistenceData data= templates[i]; | |
145 | 269 Template template_= data.getTemplate(); |
129 | 270 |
271 Node node= document.createElement(TEMPLATE_ELEMENT); | |
272 root.appendChild(node); | |
273 | |
274 NamedNodeMap attributes= node.getAttributes(); | |
275 | |
276 String id= data.getId(); | |
277 if (id !is null) { | |
278 Attr idAttr= document.createAttribute(ID_ATTRIBUTE); | |
279 idAttr.setValue(id); | |
280 attributes.setNamedItem(idAttr); | |
281 } | |
282 | |
145 | 283 if (template_ !is null) { |
129 | 284 Attr name= document.createAttribute(NAME_ATTRIBUTE); |
145 | 285 name.setValue(template_.getName()); |
129 | 286 attributes.setNamedItem(name); |
287 } | |
288 | |
145 | 289 if (template_ !is null) { |
129 | 290 Attr description= document.createAttribute(DESCRIPTION_ATTRIBUTE); |
145 | 291 description.setValue(template_.getDescription()); |
129 | 292 attributes.setNamedItem(description); |
293 } | |
294 | |
145 | 295 if (template_ !is null) { |
129 | 296 Attr context= document.createAttribute(CONTEXT_ATTRIBUTE); |
145 | 297 context.setValue(template_.getContextTypeId()); |
129 | 298 attributes.setNamedItem(context); |
299 } | |
300 | |
301 Attr enabled= document.createAttribute(ENABLED_ATTRIBUTE); | |
302 enabled.setValue(data.isEnabled() ? Boolean.toString(true) : Boolean.toString(false)); | |
303 attributes.setNamedItem(enabled); | |
304 | |
305 Attr deleted= document.createAttribute(DELETED_ATTRIBUTE); | |
306 deleted.setValue(data.isDeleted() ? Boolean.toString(true) : Boolean.toString(false)); | |
307 attributes.setNamedItem(deleted); | |
308 | |
145 | 309 if (template_ !is null) { |
129 | 310 Attr autoInsertable= document.createAttribute(AUTO_INSERTABLE_ATTRIBUTE); |
145 | 311 autoInsertable.setValue(template_.isAutoInsertable() ? Boolean.toString(true) : Boolean.toString(false)); |
129 | 312 attributes.setNamedItem(autoInsertable); |
313 } | |
314 | |
145 | 315 if (template_ !is null) { |
316 Text pattern= document.createTextNode(template_.getPattern()); | |
129 | 317 node.appendChild(pattern); |
318 } | |
319 } | |
320 | |
321 | |
322 Transformer transformer=TransformerFactory.newInstance().newTransformer(); | |
323 transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$ | |
324 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //$NON-NLS-1$ | |
325 DOMSource source = new DOMSource(document); | |
326 | |
327 transformer.transform(source, result); | |
328 | |
329 } catch (ParserConfigurationException e) { | |
330 Assert.isTrue(false); | |
331 } catch (TransformerException e) { | |
148 | 332 if (cast(IOException)e.getException() ) |
134 | 333 throw cast(IOException) e.getException(); |
129 | 334 Assert.isTrue(false); |
335 } | |
336 } | |
337 | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
338 private bool getBooleanValue(NamedNodeMap attributes, String attribute, bool defaultValue) { |
129 | 339 Node enabledNode= attributes.getNamedItem(attribute); |
340 if (enabledNode is null) | |
341 return defaultValue; | |
342 else if (enabledNode.getNodeValue().equals(Boolean.toString(true))) | |
343 return true; | |
344 else if (enabledNode.getNodeValue().equals(Boolean.toString(false))) | |
345 return false; | |
346 else | |
347 throw new SAXException(TemplatePersistenceMessages.getString("TemplateReaderWriter.error.illegal_boolean_attribute")); //$NON-NLS-1$ | |
348 } | |
349 | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
350 private String getStringValue(NamedNodeMap attributes, String name) { |
129 | 351 String val= getStringValue(attributes, name, null); |
352 if (val is null) | |
353 throw new SAXException(TemplatePersistenceMessages.getString("TemplateReaderWriter.error.missing_attribute")); //$NON-NLS-1$ | |
354 return val; | |
355 } | |
356 | |
357 private String getStringValue(NamedNodeMap attributes, String name, String defaultValue) { | |
358 Node node= attributes.getNamedItem(name); | |
359 return node is null ? defaultValue : node.getNodeValue(); | |
360 } | |
361 | |
362 private String translateString(String str, ResourceBundle bundle) { | |
363 if (bundle is null) | |
364 return str; | |
365 | |
366 int idx= str.indexOf('%'); | |
367 if (idx is -1) { | |
368 return str; | |
369 } | |
370 StringBuffer buf= new StringBuffer(); | |
371 int k= 0; | |
372 while (idx !is -1) { | |
373 buf.append(str.substring(k, idx)); | |
374 for (k= idx + 1; k < str.length() && !Character.isWhitespace(str.charAt(k)); k++) { | |
375 // loop | |
376 } | |
377 String key= str.substring(idx + 1, k); | |
378 buf.append(getBundleString(key, bundle)); | |
379 idx= str.indexOf('%', k); | |
380 } | |
381 buf.append(str.substring(k)); | |
382 return buf.toString(); | |
383 } | |
384 | |
385 private String getBundleString(String key, ResourceBundle bundle) { | |
386 if (bundle !is null) { | |
387 try { | |
388 return bundle.getString(key); | |
389 } catch (MissingResourceException e) { | |
390 return '!' + key + '!'; | |
391 } | |
392 } | |
393 return TemplatePersistenceMessages.getString(key); // default messages | |
394 } | |
395 } | |
396 |