2  * (c) Copyright IBM Corp. 2000, 2001.
 
   5 package net.sourceforge.phpeclipse.mover.obfuscator;
 
   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.HashMap;
 
  14 import java.util.Iterator;
 
  16 import javax.xml.parsers.DocumentBuilder;
 
  17 import javax.xml.parsers.DocumentBuilderFactory;
 
  18 import javax.xml.parsers.ParserConfigurationException;
 
  20 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 
  22 import org.apache.xml.serialize.OutputFormat;
 
  23 import org.apache.xml.serialize.Serializer;
 
  24 import org.apache.xml.serialize.SerializerFactory;
 
  25 import org.eclipse.core.runtime.CoreException;
 
  26 import org.w3c.dom.Document;
 
  27 import org.w3c.dom.NamedNodeMap;
 
  28 import org.w3c.dom.Node;
 
  29 import org.w3c.dom.NodeList;
 
  30 import org.w3c.dom.Text;
 
  31 import org.xml.sax.InputSource;
 
  32 import org.xml.sax.SAXException;
 
  35  * <code>ObfuscatorIgnoreSet</code> manages a collection of templates and makes them
 
  38 public class ObfuscatorIgnoreSet {
 
  40         //      private static class TemplateComparator implements Comparator {
 
  41         //              public int compare(Object arg0, Object arg1) {
 
  48         //                      Template template0= (Template) arg0;
 
  49         //                      Template template1= (Template) arg1;
 
  51         //                      return template0.getName().compareTo(template1.getName());
 
  55         private static final String TEMPLATE_TAG = "ignore"; //$NON-NLS-1$
 
  56         //      private static final String NAME_ATTRIBUTE= "name"; //$NON-NLS-1$
 
  57         //      private static final String DESCRIPTION_ATTRIBUTE= "description"; //$NON-NLS-1$
 
  58         //      private static final String CONTEXT_ATTRIBUTE= "context"; //$NON-NLS-1$
 
  59         //      private static final String ENABLED_ATTRIBUTE= "enabled"; //$NON-NLS-1$
 
  61         //      private List fTemplates= new ArrayList();
 
  62         private HashMap fIdentifierMap = new HashMap();
 
  63         //      private Comparator fTemplateComparator= new TemplateComparator();
 
  64         //      private Template[] fSortedTemplates= new Template[0];
 
  67          * Convenience method for reading templates from a file.
 
  69          * @see #addFromStream(InputStream)
 
  71         public void addFromFile(File file) throws CoreException {
 
  72                 InputStream stream = null;
 
  75                         stream = new FileInputStream(file);
 
  76                         addFromStream(stream);
 
  78                 } catch (IOException e) {
 
  79                         throwReadException(e);
 
  85                         } catch (IOException e) {
 
  91          * Reads templates from a XML stream and adds them to the template set.
 
  93         public void addFromStream(InputStream stream) throws CoreException {
 
  95                         DocumentBuilderFactory factory =
 
  96                                 DocumentBuilderFactory.newInstance();
 
  97                         DocumentBuilder parser = factory.newDocumentBuilder();
 
  98                         Document document = parser.parse(new InputSource(stream));
 
  99                         NodeList elements = document.getElementsByTagName(TEMPLATE_TAG);
 
 101                         int count = elements.getLength();
 
 102                         for (int i = 0; i != count; i++) {
 
 103                                 Node node = elements.item(i);
 
 104                                 NamedNodeMap attributes = node.getAttributes();
 
 106                                 if (attributes == null)
 
 109                                 //                              String name= getAttributeValue(attributes, NAME_ATTRIBUTE);
 
 110                                 //                              String description= getAttributeValue(attributes, DESCRIPTION_ATTRIBUTE);
 
 111                                 //                              String context= getAttributeValue(attributes, CONTEXT_ATTRIBUTE);
 
 112                                 //                              Node enabledNode= attributes.getNamedItem(ENABLED_ATTRIBUTE);
 
 114                                 //                              if (name == null || description == null || context == null)
 
 115                                 //                                      throw new SAXException(ObfuscatorMessages.getString("TemplateSet.error.missing.attribute")); //$NON-NLS-1$
 
 117                                 //                              boolean enabled= true; //(enabledNode == null) || (enabledNode.getNodeValue().equals("true")); //$NON-NLS-1$
 
 119                                 StringBuffer buffer = new StringBuffer();
 
 120                                 NodeList children = node.getChildNodes();
 
 121                                 for (int j = 0; j != children.getLength(); j++) {
 
 122                                         String value = children.item(j).getNodeValue();
 
 124                                                 buffer.append(value);
 
 126                                 String pattern = buffer.toString().trim();
 
 127                                 fIdentifierMap.put(pattern, new PHPIdentifier(pattern, PHPIdentifier.VARIABLE) );
 
 128                                 //                              Template template= new Template(name, description, context, pattern);   
 
 129                                 //                              template.setEnabled(enabled);
 
 135                 } catch (ParserConfigurationException e) {
 
 136                         throwReadException(e);
 
 137                 } catch (IOException e) {
 
 138                         throwReadException(e);
 
 139                 } catch (SAXException e) {
 
 140                         throwReadException(e);
 
 144         private String getAttributeValue(NamedNodeMap attributes, String name) {
 
 145                 Node node = attributes.getNamedItem(name);
 
 147                 return node == null ? null : node.getNodeValue();
 
 151          * Convenience method for saving to a file.
 
 153          * @see #saveToStream(OutputStream)
 
 155         public void saveToFile(File file) throws CoreException {
 
 156                 OutputStream stream = null;
 
 159                         stream = new FileOutputStream(file);
 
 160                         saveToStream(stream);
 
 162                 } catch (IOException e) {
 
 163                         throwWriteException(e);
 
 169                         } catch (IOException e) {
 
 175          * Saves the template set as XML.
 
 177         public void saveToStream(OutputStream stream) throws CoreException {
 
 179                         DocumentBuilderFactory factory =
 
 180                                 DocumentBuilderFactory.newInstance();
 
 181                         DocumentBuilder builder = factory.newDocumentBuilder();
 
 182                         Document document = builder.newDocument();
 
 184                         Node root = document.createElement("obfuscator"); // $NON-NLS-1$ //$NON-NLS-1$
 
 185                         document.appendChild(root);
 
 186                         Iterator iter = fIdentifierMap.keySet().iterator();
 
 187                         while (iter.hasNext()) {
 
 188                                 //                      for (int i= 0; i != fTemplates.size(); i++) {
 
 189                                 //                              Template template= (Template) fTemplates.get(i);
 
 191                                 Node node = document.createElement("ignore"); // $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((String) iter.next());
 
 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() {
 
 276                         fIdentifierMap.clear();
 
 277         //              fTemplates.clear();
 
 282         //       * Returns all templates.
 
 284         //      public Template[] getTemplates() {
 
 285         //              return (Template[]) fTemplates.toArray(new Template[fTemplates.size()]);
 
 289          * Returns all templates with a given name.
 
 291         //      public Template[] getTemplates(String name) {
 
 292         //              ArrayList res= new ArrayList();
 
 293         //              for (Iterator iterator= fTemplates.iterator(); iterator.hasNext();) {
 
 294         //                      Template curr= (Template) iterator.next();
 
 295         //                      if (curr.getName().equals(name)) {
 
 299         //              return (Template[]) res.toArray(new Template[res.size()]);
 
 302         //      private void sort() {
 
 303         //              fSortedTemplates= (Template[]) fTemplates.toArray(new Template[fTemplates.size()]);
 
 304         //              Arrays.sort(fSortedTemplates, fTemplateComparator);
 
 310         public HashMap getIdentifierMap() {
 
 311                 return fIdentifierMap;