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 javax.xml.parsers.DocumentBuilder;
 
  26 import javax.xml.parsers.DocumentBuilderFactory;
 
  27 import javax.xml.parsers.ParserConfigurationException;
 
  28 import javax.xml.transform.OutputKeys;
 
  29 import javax.xml.transform.Transformer;
 
  30 import javax.xml.transform.TransformerException;
 
  31 import javax.xml.transform.TransformerFactory;
 
  32 import javax.xml.transform.dom.DOMSource;
 
  33 import javax.xml.transform.stream.StreamResult;
 
  34 import org.eclipse.core.runtime.CoreException;
 
  35 import org.w3c.dom.Attr;
 
  36 import org.w3c.dom.Document;
 
  37 import org.w3c.dom.NamedNodeMap;
 
  38 import org.w3c.dom.Node;
 
  39 import org.w3c.dom.NodeList;
 
  40 import org.w3c.dom.Text;
 
  41 import org.xml.sax.InputSource;
 
  42 import org.xml.sax.SAXException;
 
  43 import org.xml.sax.SAXParseException;
 
  46  * <code>ObfuscatorIgnoreSet</code> manages a collection of templates and makes them
 
  49 public class TemplateSet {
 
  51   private static class TemplateComparator implements Comparator {
 
  52     public int compare(Object arg0, Object arg1) {
 
  59       Template template0 = (Template) arg0;
 
  60       Template template1 = (Template) arg1;
 
  62       return template0.getName().compareTo(template1.getName());
 
  66   private static final String TEMPLATE_TAG = "template"; //$NON-NLS-1$
 
  67   private static final String NAME_ATTRIBUTE = "name"; //$NON-NLS-1$
 
  68   private static final String DESCRIPTION_ATTRIBUTE = "description"; //$NON-NLS-1$
 
  69   private static final String CONTEXT_ATTRIBUTE = "context"; //$NON-NLS-1$
 
  70   private static final String ENABLED_ATTRIBUTE = "enabled"; //$NON-NLS-1$
 
  72   private List fTemplates = new ArrayList();
 
  73   private Comparator fTemplateComparator = new TemplateComparator();
 
  74   private Template[] fSortedTemplates = new Template[0];
 
  77    * Convenience method for reading templates from a file.
 
  79    * @see #addFromStream(InputStream)
 
  81   public void addFromFile(File file) throws CoreException {
 
  82     InputStream stream = null;
 
  85       stream = new FileInputStream(file);
 
  86       addFromStream(stream);
 
  88     } catch (IOException e) {
 
  89       throwReadException(e);
 
  95       } catch (IOException e) {
 
 101    * Reads templates from a XML stream and adds them to the template set.
 
 103   public void addFromStream(InputStream stream) throws CoreException {
 
 105       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 
 106       DocumentBuilder parser = factory.newDocumentBuilder();
 
 107       Document document = parser.parse(new InputSource(stream));
 
 108       NodeList elements = document.getElementsByTagName(TEMPLATE_TAG);
 
 110       int count = elements.getLength();
 
 111       for (int i = 0; i != count; i++) {
 
 112         Node node = elements.item(i);
 
 113         NamedNodeMap attributes = node.getAttributes();
 
 115         if (attributes == null)
 
 118         String name = getAttributeValue(attributes, NAME_ATTRIBUTE);
 
 119         String description = getAttributeValue(attributes, DESCRIPTION_ATTRIBUTE);
 
 120         String context = getAttributeValue(attributes, CONTEXT_ATTRIBUTE);
 
 121         Node enabledNode = attributes.getNamedItem(ENABLED_ATTRIBUTE);
 
 123         if (name == null || description == null || context == null)
 
 124           throw new SAXException(TemplateMessages.getString("TemplateSet.error.missing.attribute")); //$NON-NLS-1$
 
 126         boolean enabled = (enabledNode == null) || (enabledNode.getNodeValue().equals("true")); //$NON-NLS-1$
 
 128         StringBuffer buffer = new StringBuffer();
 
 129         NodeList children = node.getChildNodes();
 
 130         for (int j = 0; j != children.getLength(); j++) {
 
 131           String value = children.item(j).getNodeValue();
 
 133             buffer.append(value);
 
 135         String pattern = buffer.toString().trim();
 
 137         Template template = new Template(name, description, context, pattern);
 
 138         template.setEnabled(enabled);
 
 144     } catch (ParserConfigurationException e) {
 
 145       throwReadException(e);
 
 146     } catch (IOException e) {
 
 147       throwReadException(e);
 
 148     } catch (SAXParseException e) {
 
 149                         System.out.println("SAXParseException in line:"+e.getLineNumber()+" column:"+e.getColumnNumber());
 
 150       throwReadException(e);
 
 151     } catch (SAXException e) {
 
 152       throwReadException(e);
 
 156   private String getAttributeValue(NamedNodeMap attributes, String name) {
 
 157     Node node = attributes.getNamedItem(name);
 
 159     return node == null ? null : node.getNodeValue();
 
 163    * Convenience method for saving to a file.
 
 165    * @see #saveToStream(OutputStream)
 
 167   public void saveToFile(File file) throws CoreException {
 
 168     OutputStream stream = null;
 
 171       stream = new FileOutputStream(file);
 
 172       saveToStream(stream);
 
 174     } catch (IOException e) {
 
 175       throwWriteException(e);
 
 181       } catch (IOException e) {
 
 187    * Saves the template set as XML.
 
 189   public void saveToStream(OutputStream stream) throws CoreException {
 
 191       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 
 192       DocumentBuilder builder = factory.newDocumentBuilder();
 
 193       Document document = builder.newDocument();
 
 195       Node root = document.createElement("templates"); // $NON-NLS-1$ //$NON-NLS-1$
 
 196       document.appendChild(root);
 
 198       for (int i = 0; i != fTemplates.size(); i++) {
 
 199         Template template = (Template) fTemplates.get(i);
 
 201         Node node = document.createElement("template"); // $NON-NLS-1$ //$NON-NLS-1$
 
 202         root.appendChild(node);
 
 204         NamedNodeMap attributes = node.getAttributes();
 
 206         Attr name = document.createAttribute(NAME_ATTRIBUTE);
 
 207         name.setValue(template.getName());
 
 208         attributes.setNamedItem(name);
 
 210         Attr description = document.createAttribute(DESCRIPTION_ATTRIBUTE);
 
 211         description.setValue(template.getDescription());
 
 212         attributes.setNamedItem(description);
 
 214         Attr context = document.createAttribute(CONTEXT_ATTRIBUTE);
 
 215         context.setValue(template.getContextTypeName());
 
 216         attributes.setNamedItem(context);
 
 218         Attr enabled = document.createAttribute(ENABLED_ATTRIBUTE);
 
 219         enabled.setValue(template.isEnabled() ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$
 
 220         attributes.setNamedItem(enabled);
 
 222         Text pattern = document.createTextNode(template.getPattern());
 
 223         node.appendChild(pattern);
 
 226       Transformer transformer=TransformerFactory.newInstance().newTransformer();
 
 227           transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
 
 228           transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //$NON-NLS-1$
 
 229           DOMSource source = new DOMSource(document);
 
 230           StreamResult result = new StreamResult(stream);
 
 232           transformer.transform(source, result);
 
 234 //      OutputFormat format = new OutputFormat();
 
 235 //      format.setPreserveSpace(true);
 
 236 //      Serializer serializer = SerializerFactory.getSerializerFactory("xml").makeSerializer(stream, format); //$NON-NLS-1$
 
 237 //      serializer.asDOMSerializer().serialize(document);
 
 239     } catch (ParserConfigurationException e) {
 
 240       throwWriteException(e);
 
 241     } catch (TransformerException e) {
 
 242                 throwWriteException(e);
 
 244 //    } catch (IOException e) {
 
 245 //      throwWriteException(e);
 
 249   private static void throwReadException(Throwable t) throws CoreException {
 
 250     PHPeclipsePlugin.log(t);
 
 251     //          IStatus status= new JavaUIStatus(JavaStatusConstants.TEMPLATE_IO_EXCEPTION,
 
 252     //                  ObfuscatorMessages.getString("TemplateSet.error.read"), t); //$NON-NLS-1$
 
 253     //          throw new JavaUIException(status);
 
 256   private static void throwWriteException(Throwable t) throws CoreException {
 
 257     PHPeclipsePlugin.log(t);
 
 258     //          IStatus status= new JavaUIStatus(JavaStatusConstants.TEMPLATE_IO_EXCEPTION,
 
 259     //                  ObfuscatorMessages.getString("TemplateSet.error.write"), t); //$NON-NLS-1$
 
 260     //          throw new JavaUIException(status);
 
 264    * Adds a template to the set.
 
 266   public void add(Template template) {
 
 267     if (exists(template))
 
 268       return; // ignore duplicate
 
 270     fTemplates.add(template);
 
 274   private boolean exists(Template template) {
 
 275     for (Iterator iterator = fTemplates.iterator(); iterator.hasNext();) {
 
 276       Template anotherTemplate = (Template) iterator.next();
 
 278       if (template.equals(anotherTemplate))
 
 286    * Removes a template to the set.
 
 288   public void remove(Template template) {
 
 289     fTemplates.remove(template);
 
 296   public void clear() {
 
 302    * Returns all templates.
 
 304   public Template[] getTemplates() {
 
 305     return (Template[]) fTemplates.toArray(new Template[fTemplates.size()]);
 
 309    * Returns all templates with a given name.
 
 311   public Template[] getTemplates(String name) {
 
 312     ArrayList res = new ArrayList();
 
 313     for (Iterator iterator = fTemplates.iterator(); iterator.hasNext();) {
 
 314       Template curr = (Template) iterator.next();
 
 315       if (curr.getName().equals(name)) {
 
 319     return (Template[]) res.toArray(new Template[res.size()]);
 
 322   private void sort() {
 
 323     fSortedTemplates = (Template[]) fTemplates.toArray(new Template[fTemplates.size()]);
 
 324     Arrays.sort(fSortedTemplates, fTemplateComparator);