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