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.eclipse.core.runtime.IStatus;
30 import org.w3c.dom.Attr;
31 import org.w3c.dom.Document;
32 import org.w3c.dom.NamedNodeMap;
33 import org.w3c.dom.Node;
34 import org.w3c.dom.NodeList;
35 import org.w3c.dom.Text;
36 import org.xml.sax.InputSource;
37 import org.xml.sax.SAXException;
38 import org.xml.sax.SAXParseException;
41 * <code>ObfuscatorIgnoreSet</code> manages a collection of templates and makes them
44 public class TemplateSet {
46 private static class TemplateComparator implements Comparator {
47 public int compare(Object arg0, Object arg1) {
54 Template template0 = (Template) arg0;
55 Template template1 = (Template) arg1;
57 return template0.getName().compareTo(template1.getName());
61 private static final String TEMPLATE_TAG = "template"; //$NON-NLS-1$
62 private static final String NAME_ATTRIBUTE = "name"; //$NON-NLS-1$
63 private static final String DESCRIPTION_ATTRIBUTE = "description"; //$NON-NLS-1$
64 private static final String CONTEXT_ATTRIBUTE = "context"; //$NON-NLS-1$
65 private static final String ENABLED_ATTRIBUTE = "enabled"; //$NON-NLS-1$
67 private List fTemplates = new ArrayList();
68 private Comparator fTemplateComparator = new TemplateComparator();
69 private Template[] fSortedTemplates = new Template[0];
72 * Convenience method for reading templates from a file.
74 * @see #addFromStream(InputStream)
76 public void addFromFile(File file) throws CoreException {
77 InputStream stream = null;
80 stream = new FileInputStream(file);
81 addFromStream(stream);
83 } catch (IOException e) {
84 throwReadException(e);
90 } 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 (SAXParseException e) {
144 System.out.println("SAXParseException in line:"+e.getLineNumber()+" column:"+e.getColumnNumber());
145 throwReadException(e);
146 } catch (SAXException e) {
147 throwReadException(e);
151 private String getAttributeValue(NamedNodeMap attributes, String name) {
152 Node node = attributes.getNamedItem(name);
154 return node == null ? null : node.getNodeValue();
158 * Convenience method for saving to a file.
160 * @see #saveToStream(OutputStream)
162 public void saveToFile(File file) throws CoreException {
163 OutputStream stream = null;
166 stream = new FileOutputStream(file);
167 saveToStream(stream);
169 } catch (IOException e) {
170 throwWriteException(e);
176 } catch (IOException e) {
182 * Saves the template set as XML.
184 public void saveToStream(OutputStream stream) throws CoreException {
186 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
187 DocumentBuilder builder = factory.newDocumentBuilder();
188 Document document = builder.newDocument();
190 Node root = document.createElement("templates"); // $NON-NLS-1$ //$NON-NLS-1$
191 document.appendChild(root);
193 for (int i = 0; i != fTemplates.size(); i++) {
194 Template template = (Template) fTemplates.get(i);
196 Node node = document.createElement("template"); // $NON-NLS-1$ //$NON-NLS-1$
197 root.appendChild(node);
199 NamedNodeMap attributes = node.getAttributes();
201 Attr name = document.createAttribute(NAME_ATTRIBUTE);
202 name.setValue(template.getName());
203 attributes.setNamedItem(name);
205 Attr description = document.createAttribute(DESCRIPTION_ATTRIBUTE);
206 description.setValue(template.getDescription());
207 attributes.setNamedItem(description);
209 Attr context = document.createAttribute(CONTEXT_ATTRIBUTE);
210 context.setValue(template.getContextTypeName());
211 attributes.setNamedItem(context);
213 Attr enabled = document.createAttribute(ENABLED_ATTRIBUTE);
214 enabled.setValue(template.isEnabled() ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$
215 attributes.setNamedItem(enabled);
217 Text pattern = document.createTextNode(template.getPattern());
218 node.appendChild(pattern);
221 OutputFormat format = new OutputFormat();
222 format.setPreserveSpace(true);
223 Serializer serializer = SerializerFactory.getSerializerFactory("xml").makeSerializer(stream, format); //$NON-NLS-1$
224 serializer.asDOMSerializer().serialize(document);
226 } catch (ParserConfigurationException e) {
227 throwWriteException(e);
228 } catch (IOException e) {
229 throwWriteException(e);
233 private static void throwReadException(Throwable t) throws CoreException {
234 PHPeclipsePlugin.log(t);
235 // IStatus status= new JavaUIStatus(JavaStatusConstants.TEMPLATE_IO_EXCEPTION,
236 // ObfuscatorMessages.getString("TemplateSet.error.read"), t); //$NON-NLS-1$
237 // throw new JavaUIException(status);
240 private static void throwWriteException(Throwable t) throws CoreException {
241 PHPeclipsePlugin.log(t);
242 // IStatus status= new JavaUIStatus(JavaStatusConstants.TEMPLATE_IO_EXCEPTION,
243 // ObfuscatorMessages.getString("TemplateSet.error.write"), t); //$NON-NLS-1$
244 // throw new JavaUIException(status);
248 * Adds a template to the set.
250 public void add(Template template) {
251 if (exists(template))
252 return; // ignore duplicate
254 fTemplates.add(template);
258 private boolean exists(Template template) {
259 for (Iterator iterator = fTemplates.iterator(); iterator.hasNext();) {
260 Template anotherTemplate = (Template) iterator.next();
262 if (template.equals(anotherTemplate))
270 * Removes a template to the set.
272 public void remove(Template template) {
273 fTemplates.remove(template);
280 public void clear() {
286 * Returns all templates.
288 public Template[] getTemplates() {
289 return (Template[]) fTemplates.toArray(new Template[fTemplates.size()]);
293 * Returns all templates with a given name.
295 public Template[] getTemplates(String name) {
296 ArrayList res = new ArrayList();
297 for (Iterator iterator = fTemplates.iterator(); iterator.hasNext();) {
298 Template curr = (Template) iterator.next();
299 if (curr.getName().equals(name)) {
303 return (Template[]) res.toArray(new Template[res.size()]);
306 private void sort() {
307 fSortedTemplates = (Template[]) fTemplates.toArray(new Template[fTemplates.size()]);
308 Arrays.sort(fSortedTemplates, fTemplateComparator);