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