2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.corext.template;
8 import java.io.FileInputStream;
9 import java.io.FileOutputStream;
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.io.OutputStream;
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.Comparator;
16 import java.util.Iterator;
17 import java.util.List;
19 import javax.xml.parsers.DocumentBuilder;
20 import javax.xml.parsers.DocumentBuilderFactory;
21 import javax.xml.parsers.ParserConfigurationException;
23 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
24 import org.apache.xml.serialize.OutputFormat;
25 import org.apache.xml.serialize.Serializer;
26 import org.apache.xml.serialize.SerializerFactory;
27 import org.eclipse.core.runtime.CoreException;
28 import org.w3c.dom.Attr;
29 import org.w3c.dom.Document;
30 import org.w3c.dom.NamedNodeMap;
31 import org.w3c.dom.Node;
32 import org.w3c.dom.NodeList;
33 import org.w3c.dom.Text;
34 import org.xml.sax.InputSource;
35 import org.xml.sax.SAXException;
36 //import org.eclipse.jdt.internal.ui.JavaUIStatus;
37 //import org.eclipse.jdt.internal.ui.JavaUIException;
39 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
42 * <code>TemplateSet</code> manages a collection of templates and makes them
45 public class TemplateSet {
47 private static class TemplateComparator implements Comparator {
48 public int compare(Object arg0, Object arg1) {
55 Template template0= (Template) arg0;
56 Template template1= (Template) arg1;
58 return template0.getName().compareTo(template1.getName());
62 private static final String TEMPLATE_TAG= "template"; //$NON-NLS-1$
63 private static final String NAME_ATTRIBUTE= "name"; //$NON-NLS-1$
64 private static final String DESCRIPTION_ATTRIBUTE= "description"; //$NON-NLS-1$
65 private static final String CONTEXT_ATTRIBUTE= "context"; //$NON-NLS-1$
66 private static final String ENABLED_ATTRIBUTE= "enabled"; //$NON-NLS-1$
68 private List fTemplates= new ArrayList();
69 private Comparator fTemplateComparator= new TemplateComparator();
70 private Template[] fSortedTemplates= new Template[0];
73 * Convenience method for reading templates from a file.
75 * @see #addFromStream(InputStream)
77 public void addFromFile(File file) throws CoreException {
78 InputStream stream= null;
81 stream= new FileInputStream(file);
82 addFromStream(stream);
84 } catch (IOException e) {
85 throwReadException(e);
91 } catch (IOException e) {}
96 * Reads templates from a XML stream and adds them to the template set.
98 public void addFromStream(InputStream stream) throws CoreException {
100 DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance();
101 DocumentBuilder parser= factory.newDocumentBuilder();
102 Document document= parser.parse(new InputSource(stream));
103 NodeList elements= document.getElementsByTagName(TEMPLATE_TAG);
105 int count= elements.getLength();
106 for (int i= 0; i != count; i++) {
107 Node node= elements.item(i);
108 NamedNodeMap attributes= node.getAttributes();
110 if (attributes == null)
113 String name= getAttributeValue(attributes, NAME_ATTRIBUTE);
114 String description= getAttributeValue(attributes, DESCRIPTION_ATTRIBUTE);
115 String context= getAttributeValue(attributes, CONTEXT_ATTRIBUTE);
116 Node enabledNode= attributes.getNamedItem(ENABLED_ATTRIBUTE);
118 if (name == null || description == null || context == null)
119 throw new SAXException(TemplateMessages.getString("TemplateSet.error.missing.attribute")); //$NON-NLS-1$
121 boolean enabled= (enabledNode == null) || (enabledNode.getNodeValue().equals("true")); //$NON-NLS-1$
123 StringBuffer buffer= new StringBuffer();
124 NodeList children= node.getChildNodes();
125 for (int j= 0; j != children.getLength(); j++) {
126 String value= children.item(j).getNodeValue();
128 buffer.append(value);
130 String pattern= buffer.toString().trim();
132 Template template= new Template(name, description, context, pattern);
133 template.setEnabled(enabled);
139 } catch (ParserConfigurationException e) {
140 throwReadException(e);
141 } catch (IOException e) {
142 throwReadException(e);
143 } catch (SAXException e) {
144 throwReadException(e);
148 private String getAttributeValue(NamedNodeMap attributes, String name) {
149 Node node= attributes.getNamedItem(name);
153 : node.getNodeValue();
157 * Convenience method for saving to a file.
159 * @see #saveToStream(OutputStream)
161 public void saveToFile(File file) throws CoreException {
162 OutputStream stream= null;
165 stream= new FileOutputStream(file);
166 saveToStream(stream);
168 } catch (IOException e) {
169 throwWriteException(e);
175 } catch (IOException e) {}
180 * Saves the template set as XML.
182 public void saveToStream(OutputStream stream) throws CoreException {
184 DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance();
185 DocumentBuilder builder= factory.newDocumentBuilder();
186 Document document= builder.newDocument();
188 Node root= document.createElement("templates"); // $NON-NLS-1$ //$NON-NLS-1$
189 document.appendChild(root);
191 for (int i= 0; i != fTemplates.size(); i++) {
192 Template template= (Template) fTemplates.get(i);
194 Node node= document.createElement("template"); // $NON-NLS-1$ //$NON-NLS-1$
195 root.appendChild(node);
197 NamedNodeMap attributes= node.getAttributes();
199 Attr name= document.createAttribute(NAME_ATTRIBUTE);
200 name.setValue(template.getName());
201 attributes.setNamedItem(name);
203 Attr description= document.createAttribute(DESCRIPTION_ATTRIBUTE);
204 description.setValue(template.getDescription());
205 attributes.setNamedItem(description);
207 Attr context= document.createAttribute(CONTEXT_ATTRIBUTE);
208 context.setValue(template.getContextTypeName());
209 attributes.setNamedItem(context);
211 Attr enabled= document.createAttribute(ENABLED_ATTRIBUTE);
212 enabled.setValue(template.isEnabled() ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$
213 attributes.setNamedItem(enabled);
215 Text pattern= document.createTextNode(template.getPattern());
216 node.appendChild(pattern);
219 OutputFormat format = new OutputFormat();
220 format.setPreserveSpace(true);
221 Serializer serializer = SerializerFactory.getSerializerFactory("xml").makeSerializer(stream, format); //$NON-NLS-1$
222 serializer.asDOMSerializer().serialize(document);
224 } catch (ParserConfigurationException e) {
225 throwWriteException(e);
226 } catch (IOException e) {
227 throwWriteException(e);
231 private static void throwReadException(Throwable t) throws CoreException {
232 PHPeclipsePlugin.log(t);
233 // IStatus status= new JavaUIStatus(JavaStatusConstants.TEMPLATE_IO_EXCEPTION,
234 // TemplateMessages.getString("TemplateSet.error.read"), t); //$NON-NLS-1$
235 // throw new JavaUIException(status);
238 private static void throwWriteException(Throwable t) throws CoreException {
239 PHPeclipsePlugin.log(t);
240 // IStatus status= new JavaUIStatus(JavaStatusConstants.TEMPLATE_IO_EXCEPTION,
241 // TemplateMessages.getString("TemplateSet.error.write"), t); //$NON-NLS-1$
242 // throw new JavaUIException(status);
246 * Adds a template to the set.
248 public void add(Template template) {
249 if (exists(template))
250 return; // ignore duplicate
252 fTemplates.add(template);
256 private boolean exists(Template template) {
257 for (Iterator iterator = fTemplates.iterator(); iterator.hasNext();) {
258 Template anotherTemplate = (Template) iterator.next();
260 if (template.equals(anotherTemplate))
268 * Removes a template to the set.
270 public void remove(Template template) {
271 fTemplates.remove(template);
278 public void clear() {
284 * Returns all templates.
286 public Template[] getTemplates() {
287 return (Template[]) fTemplates.toArray(new Template[fTemplates.size()]);
291 * Returns all templates with a given name.
293 public Template[] getTemplates(String name) {
294 ArrayList res= new ArrayList();
295 for (Iterator iterator= fTemplates.iterator(); iterator.hasNext();) {
296 Template curr= (Template) iterator.next();
297 if (curr.getName().equals(name)) {
301 return (Template[]) res.toArray(new Template[res.size()]);
304 private void sort() {
305 fSortedTemplates= (Template[]) fTemplates.toArray(new Template[fTemplates.size()]);
306 Arrays.sort(fSortedTemplates, fTemplateComparator);