bd853fcd5129813b089cb0ac3f6c6a53d7788b64
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / mover / obfuscator / ObfuscatorIgnoreSet.java
1 /*
2  * (c) Copyright IBM Corp. 2000, 2001.
3  * All Rights Reserved.
4  */
5 package net.sourceforge.phpeclipse.mover.obfuscator;
6
7 import java.io.File;
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;
15
16 import javax.xml.parsers.DocumentBuilder;
17 import javax.xml.parsers.DocumentBuilderFactory;
18 import javax.xml.parsers.ParserConfigurationException;
19
20 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
21
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;
33
34 /**
35  * <code>ObfuscatorIgnoreSet</code> manages a collection of templates and makes them
36  * persistent.
37  */
38 public class ObfuscatorIgnoreSet {
39
40         //      private static class TemplateComparator implements Comparator {
41         //              public int compare(Object arg0, Object arg1) {
42         //                      if (arg0 == arg1)
43         //                              return 0;
44         //                      
45         //                      if (arg0 == null)
46         //                              return -1;
47         //                              
48         //                      Template template0= (Template) arg0;
49         //                      Template template1= (Template) arg1;
50         //                      
51         //                      return template0.getName().compareTo(template1.getName());
52         //              }
53         //      }
54
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$
60
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];
65
66         /**
67          * Convenience method for reading templates from a file.
68          * 
69          * @see #addFromStream(InputStream)
70          */
71         public void addFromFile(File file) throws CoreException {
72                 InputStream stream = null;
73
74                 try {
75                         stream = new FileInputStream(file);
76                         addFromStream(stream);
77
78                 } catch (IOException e) {
79                         throwReadException(e);
80
81                 } finally {
82                         try {
83                                 if (stream != null)
84                                         stream.close();
85                         } catch (IOException e) {
86                         }
87                 }
88         }
89
90         /**
91          * Reads templates from a XML stream and adds them to the template set.
92          */
93         public void addFromStream(InputStream stream) throws CoreException {
94                 try {
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);
100
101                         int count = elements.getLength();
102                         for (int i = 0; i != count; i++) {
103                                 Node node = elements.item(i);
104                                 NamedNodeMap attributes = node.getAttributes();
105
106                                 if (attributes == null)
107                                         continue;
108
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);
113
114                                 //                              if (name == null || description == null || context == null)
115                                 //                                      throw new SAXException(ObfuscatorMessages.getString("TemplateSet.error.missing.attribute")); //$NON-NLS-1$
116
117                                 //                              boolean enabled= true; //(enabledNode == null) || (enabledNode.getNodeValue().equals("true")); //$NON-NLS-1$
118
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();
123                                         if (value != null)
124                                                 buffer.append(value);
125                                 }
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);
130                                 //                              add(template);
131                         }
132
133                         //                      sort();
134
135                 } catch (ParserConfigurationException e) {
136                         throwReadException(e);
137                 } catch (IOException e) {
138                         throwReadException(e);
139                 } catch (SAXException e) {
140                         throwReadException(e);
141                 }
142         }
143
144         private String getAttributeValue(NamedNodeMap attributes, String name) {
145                 Node node = attributes.getNamedItem(name);
146
147                 return node == null ? null : node.getNodeValue();
148         }
149
150         /**
151          * Convenience method for saving to a file.
152          * 
153          * @see #saveToStream(OutputStream)
154          */
155         public void saveToFile(File file) throws CoreException {
156                 OutputStream stream = null;
157
158                 try {
159                         stream = new FileOutputStream(file);
160                         saveToStream(stream);
161
162                 } catch (IOException e) {
163                         throwWriteException(e);
164
165                 } finally {
166                         try {
167                                 if (stream != null)
168                                         stream.close();
169                         } catch (IOException e) {
170                         }
171                 }
172         }
173
174         /**
175          * Saves the template set as XML.
176          */
177         public void saveToStream(OutputStream stream) throws CoreException {
178                 try {
179                         DocumentBuilderFactory factory =
180                                 DocumentBuilderFactory.newInstance();
181                         DocumentBuilder builder = factory.newDocumentBuilder();
182                         Document document = builder.newDocument();
183
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);
190
191                                 Node node = document.createElement("ignore"); // $NON-NLS-1$ //$NON-NLS-1$
192                                 root.appendChild(node);
193
194                                 //                              NamedNodeMap attributes= node.getAttributes();
195                                 //                              
196                                 //                              Attr name= document.createAttribute(NAME_ATTRIBUTE);
197                                 //                              name.setValue(template.getName());
198                                 //                              attributes.setNamedItem(name);
199                                 //      
200                                 //                              Attr description= document.createAttribute(DESCRIPTION_ATTRIBUTE);
201                                 //                              description.setValue(template.getDescription());
202                                 //                              attributes.setNamedItem(description);
203                                 //      
204                                 //                              Attr context= document.createAttribute(CONTEXT_ATTRIBUTE);
205                                 //                              context.setValue(template.getContextTypeName());
206                                 //                              attributes.setNamedItem(context);                       
207                                 //
208                                 //                              Attr enabled= document.createAttribute(ENABLED_ATTRIBUTE);
209                                 //                              enabled.setValue(template.isEnabled() ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$
210                                 //                              attributes.setNamedItem(enabled);
211
212                                 Text pattern = document.createTextNode((String) iter.next());
213                                 node.appendChild(pattern);
214                         }
215
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);
220
221                 } catch (ParserConfigurationException e) {
222                         throwWriteException(e);
223                 } catch (IOException e) {
224                         throwWriteException(e);
225                 }
226         }
227
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);
233         }
234
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);
240         }
241
242         /**
243          * Adds a template to the set.
244          */
245         //      public void add(Template template) {
246         //              if (exists(template))
247         //                      return; // ignore duplicate
248         //              
249         //              fTemplates.add(template);
250         //              sort();
251         //      }
252
253         //      private boolean exists(Template template) {
254         //              for (Iterator iterator = fTemplates.iterator(); iterator.hasNext();) {
255         //                      Template anotherTemplate = (Template) iterator.next();
256         //
257         //                      if (template.equals(anotherTemplate))
258         //                              return true;
259         //              }
260         //              
261         //              return false;
262         //      }
263         //      
264         //      /**
265         //       * Removes a template to the set.
266         //       */     
267         //      public void remove(Template template) {
268         //              fTemplates.remove(template);
269         //              sort();
270         //      }
271         //
272                 /**
273                  * Empties the set.
274                  */             
275                 public void clear() {
276                         fIdentifierMap.clear();
277         //              fTemplates.clear();
278         //              sort();
279                 }
280         //      
281         //      /**
282         //       * Returns all templates.
283         //       */
284         //      public Template[] getTemplates() {
285         //              return (Template[]) fTemplates.toArray(new Template[fTemplates.size()]);
286         //      }
287
288         /**
289          * Returns all templates with a given name.
290          */
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)) {
296         //                              res.add(curr);
297         //                      }
298         //              }
299         //              return (Template[]) res.toArray(new Template[res.size()]);
300         //      }       
301         //      
302         //      private void sort() {
303         //              fSortedTemplates= (Template[]) fTemplates.toArray(new Template[fTemplates.size()]);
304         //              Arrays.sort(fSortedTemplates, fTemplateComparator);
305         //      }
306
307         /**
308          * @return
309          */
310         public HashMap getIdentifierMap() {
311                 return fIdentifierMap;
312         }
313
314 }