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;
25 import org.apache.xml.serialize.OutputFormat;
26 import org.apache.xml.serialize.Serializer;
27 import org.apache.xml.serialize.SerializerFactory;
28 import org.eclipse.core.runtime.CoreException;
29 import org.w3c.dom.Attr;
30 import org.w3c.dom.Document;
31 import org.w3c.dom.NamedNodeMap;
32 import org.w3c.dom.Node;
33 import org.w3c.dom.NodeList;
34 import org.w3c.dom.Text;
35 import org.xml.sax.InputSource;
36 import org.xml.sax.SAXException;
39 * <code>ObfuscatorIgnoreSet</code> manages a collection of templates and makes them
42 public class TemplateSet {
44 private static class TemplateComparator implements Comparator {
45 public int compare(Object arg0, Object arg1) {
52 Template template0= (Template) arg0;
53 Template template1= (Template) arg1;
55 return template0.getName().compareTo(template1.getName());
59 private static final String TEMPLATE_TAG= "template"; //$NON-NLS-1$
60 private static final String NAME_ATTRIBUTE= "name"; //$NON-NLS-1$
61 private static final String DESCRIPTION_ATTRIBUTE= "description"; //$NON-NLS-1$
62 private static final String CONTEXT_ATTRIBUTE= "context"; //$NON-NLS-1$
63 private static final String ENABLED_ATTRIBUTE= "enabled"; //$NON-NLS-1$
65 private List fTemplates= new ArrayList();
66 private Comparator fTemplateComparator= new TemplateComparator();
67 private Template[] fSortedTemplates= new Template[0];
70 * Convenience method for reading templates from a file.
72 * @see #addFromStream(InputStream)
74 public void addFromFile(File file) throws CoreException {
75 InputStream stream= null;
78 stream= new FileInputStream(file);
79 addFromStream(stream);
81 } catch (IOException e) {
82 throwReadException(e);
88 } catch (IOException e) {}
93 * Reads templates from a XML stream and adds them to the template set.
95 public void addFromStream(InputStream stream) throws CoreException {
97 DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance();
98 DocumentBuilder parser= factory.newDocumentBuilder();
99 Document document= parser.parse(new InputSource(stream));
100 NodeList elements= document.getElementsByTagName(TEMPLATE_TAG);
102 int count= elements.getLength();
103 for (int i= 0; i != count; i++) {
104 Node node= elements.item(i);
105 NamedNodeMap attributes= node.getAttributes();
107 if (attributes == null)
110 String name= getAttributeValue(attributes, NAME_ATTRIBUTE);
111 String description= getAttributeValue(attributes, DESCRIPTION_ATTRIBUTE);
112 String context= getAttributeValue(attributes, CONTEXT_ATTRIBUTE);
113 Node enabledNode= attributes.getNamedItem(ENABLED_ATTRIBUTE);
115 if (name == null || description == null || context == null)
116 throw new SAXException(TemplateMessages.getString("TemplateSet.error.missing.attribute")); //$NON-NLS-1$
118 boolean enabled= (enabledNode == null) || (enabledNode.getNodeValue().equals("true")); //$NON-NLS-1$
120 StringBuffer buffer= new StringBuffer();
121 NodeList children= node.getChildNodes();
122 for (int j= 0; j != children.getLength(); j++) {
123 String value= children.item(j).getNodeValue();
125 buffer.append(value);
127 String pattern= buffer.toString().trim();
129 Template template= new Template(name, description, context, pattern);
130 template.setEnabled(enabled);
136 } catch (ParserConfigurationException e) {
137 throwReadException(e);
138 } catch (IOException e) {
139 throwReadException(e);
140 } catch (SAXException e) {
141 throwReadException(e);
145 private String getAttributeValue(NamedNodeMap attributes, String name) {
146 Node node= attributes.getNamedItem(name);
150 : node.getNodeValue();
154 * Convenience method for saving to a file.
156 * @see #saveToStream(OutputStream)
158 public void saveToFile(File file) throws CoreException {
159 OutputStream stream= null;
162 stream= new FileOutputStream(file);
163 saveToStream(stream);
165 } catch (IOException e) {
166 throwWriteException(e);
172 } catch (IOException e) {}
177 * Saves the template set as XML.
179 public void saveToStream(OutputStream stream) throws CoreException {
181 DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance();
182 DocumentBuilder builder= factory.newDocumentBuilder();
183 Document document= builder.newDocument();
185 Node root= document.createElement("templates"); // $NON-NLS-1$ //$NON-NLS-1$
186 document.appendChild(root);
188 for (int i= 0; i != fTemplates.size(); i++) {
189 Template template= (Template) fTemplates.get(i);
191 Node node= document.createElement("template"); // $NON-NLS-1$ //$NON-NLS-1$
192 root.appendChild(node);
194 NamedNodeMap attributes= node.getAttributes();
196 Attr name= document.createAttribute(NAME_ATTRIBUTE);
197 name.setValue(template.getName());
198 attributes.setNamedItem(name);
200 Attr description= document.createAttribute(DESCRIPTION_ATTRIBUTE);
201 description.setValue(template.getDescription());
202 attributes.setNamedItem(description);
204 Attr context= document.createAttribute(CONTEXT_ATTRIBUTE);
205 context.setValue(template.getContextTypeName());
206 attributes.setNamedItem(context);
208 Attr enabled= document.createAttribute(ENABLED_ATTRIBUTE);
209 enabled.setValue(template.isEnabled() ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$
210 attributes.setNamedItem(enabled);
212 Text pattern= document.createTextNode(template.getPattern());
213 node.appendChild(pattern);
216 OutputFormat format = new OutputFormat();
217 format.setPreserveSpace(true);
218 Serializer serializer = SerializerFactory.getSerializerFactory("xml").makeSerializer(stream, format); //$NON-NLS-1$
219 serializer.asDOMSerializer().serialize(document);
221 } catch (ParserConfigurationException e) {
222 throwWriteException(e);
223 } catch (IOException e) {
224 throwWriteException(e);
228 private static void throwReadException(Throwable t) throws CoreException {
229 PHPeclipsePlugin.log(t);
230 // IStatus status= new JavaUIStatus(JavaStatusConstants.TEMPLATE_IO_EXCEPTION,
231 // ObfuscatorMessages.getString("TemplateSet.error.read"), t); //$NON-NLS-1$
232 // throw new JavaUIException(status);
235 private static void throwWriteException(Throwable t) throws CoreException {
236 PHPeclipsePlugin.log(t);
237 // IStatus status= new JavaUIStatus(JavaStatusConstants.TEMPLATE_IO_EXCEPTION,
238 // ObfuscatorMessages.getString("TemplateSet.error.write"), t); //$NON-NLS-1$
239 // throw new JavaUIException(status);
243 * Adds a template to the set.
245 public void add(Template template) {
246 if (exists(template))
247 return; // ignore duplicate
249 fTemplates.add(template);
253 private boolean exists(Template template) {
254 for (Iterator iterator = fTemplates.iterator(); iterator.hasNext();) {
255 Template anotherTemplate = (Template) iterator.next();
257 if (template.equals(anotherTemplate))
265 * Removes a template to the set.
267 public void remove(Template template) {
268 fTemplates.remove(template);
275 public void clear() {
281 * Returns all templates.
283 public Template[] getTemplates() {
284 return (Template[]) fTemplates.toArray(new Template[fTemplates.size()]);
288 * Returns all templates with a given name.
290 public Template[] getTemplates(String name) {
291 ArrayList res= new ArrayList();
292 for (Iterator iterator= fTemplates.iterator(); iterator.hasNext();) {
293 Template curr= (Template) iterator.next();
294 if (curr.getName().equals(name)) {
298 return (Template[]) res.toArray(new Template[res.size()]);
301 private void sort() {
302 fSortedTemplates= (Template[]) fTemplates.toArray(new Template[fTemplates.size()]);
303 Arrays.sort(fSortedTemplates, fTemplateComparator);