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