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;
|
|
27 import java.util.ArrayList;
|
|
28 import java.util.Collection;
|
|
29 import java.util.HashSet;
|
|
30 import java.util.MissingResourceException;
|
|
31 import java.util.ResourceBundle;
|
|
32 import java.util.Set;
|
|
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
|
|
68 private static final String TEMPLATE_ROOT = "templates"; //$NON-NLS-1$
|
|
69 private static final String TEMPLATE_ELEMENT = "template"; //$NON-NLS-1$
|
|
70 private static final String NAME_ATTRIBUTE= "name"; //$NON-NLS-1$
|
|
71 private static final String ID_ATTRIBUTE= "id"; //$NON-NLS-1$
|
|
72 private static final String DESCRIPTION_ATTRIBUTE= "description"; //$NON-NLS-1$
|
|
73 private static final String CONTEXT_ATTRIBUTE= "context"; //$NON-NLS-1$
|
|
74 private static final String ENABLED_ATTRIBUTE= "enabled"; //$NON-NLS-1$
|
|
75 private static final String DELETED_ATTRIBUTE= "deleted"; //$NON-NLS-1$
|
|
76 /**
|
|
77 * @since 3.1
|
|
78 */
|
|
79 private static final String AUTO_INSERTABLE_ATTRIBUTE= "autoinsert"; //$NON-NLS-1$
|
|
80
|
|
81 /**
|
|
82 * Create a new instance.
|
|
83 */
|
|
84 public TemplateReaderWriter() {
|
|
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 */
|
|
95 public TemplatePersistenceData[] read(Reader reader) throws IOException {
|
|
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 */
|
|
111 public TemplatePersistenceData readSingle(Reader reader, String id) throws IOException {
|
|
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 */
|
|
126 public TemplatePersistenceData[] read(Reader reader, ResourceBundle bundle) throws IOException {
|
|
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 */
|
|
138 public TemplatePersistenceData[] read(InputStream stream, ResourceBundle bundle) throws IOException {
|
|
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 */
|
|
151 private TemplatePersistenceData[] read(InputSource source, ResourceBundle bundle, String singleId) throws IOException {
|
|
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
|
|
203 Template template= new Template(name, description, context, pattern, autoInsertable);
|
|
204 TemplatePersistenceData data= new TemplatePersistenceData(template, enabled, id);
|
|
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();
|
|
219 if (t instanceof IOException)
|
|
220 throw (IOException) t;
|
|
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 */
|
|
237 public void save(TemplatePersistenceData[] templates, OutputStream stream) throws IOException {
|
|
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 */
|
|
248 public void save(TemplatePersistenceData[] templates, Writer writer) throws IOException {
|
|
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 */
|
|
259 private void save(TemplatePersistenceData[] templates, StreamResult result) throws IOException {
|
|
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];
|
|
270 Template template= data.getTemplate();
|
|
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
|
|
284 if (template !is null) {
|
|
285 Attr name= document.createAttribute(NAME_ATTRIBUTE);
|
|
286 name.setValue(template.getName());
|
|
287 attributes.setNamedItem(name);
|
|
288 }
|
|
289
|
|
290 if (template !is null) {
|
|
291 Attr description= document.createAttribute(DESCRIPTION_ATTRIBUTE);
|
|
292 description.setValue(template.getDescription());
|
|
293 attributes.setNamedItem(description);
|
|
294 }
|
|
295
|
|
296 if (template !is null) {
|
|
297 Attr context= document.createAttribute(CONTEXT_ATTRIBUTE);
|
|
298 context.setValue(template.getContextTypeId());
|
|
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
|
|
310 if (template !is null) {
|
|
311 Attr autoInsertable= document.createAttribute(AUTO_INSERTABLE_ATTRIBUTE);
|
|
312 autoInsertable.setValue(template.isAutoInsertable() ? Boolean.toString(true) : Boolean.toString(false));
|
|
313 attributes.setNamedItem(autoInsertable);
|
|
314 }
|
|
315
|
|
316 if (template !is null) {
|
|
317 Text pattern= document.createTextNode(template.getPattern());
|
|
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) {
|
|
333 if (e.getException() instanceof IOException)
|
|
334 throw (IOException) e.getException();
|
|
335 Assert.isTrue(false);
|
|
336 }
|
|
337 }
|
|
338
|
|
339 private bool getBooleanValue(NamedNodeMap attributes, String attribute, bool defaultValue) throws SAXException {
|
|
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
|
|
351 private String getStringValue(NamedNodeMap attributes, String name) throws SAXException {
|
|
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
|