Changes:
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / obfuscator / ObfuscatorIgnoreSet.java
1 /*
2  * (c) Copyright IBM Corp. 2000, 2001.
3  * All Rights Reserved.
4  */
5 package net.sourceforge.phpeclipse.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                 } catch (IOException e) {
162                         throwWriteException(e);
163
164                 } finally {
165                         try {
166                                 if (stream != null)
167                                         stream.close();
168                         } catch (IOException e) {
169                         }
170                 }
171         }
172
173         /**
174          * Saves the template set as XML.
175          */
176         public void saveToStream(OutputStream stream) throws CoreException {
177                 try {
178                         DocumentBuilderFactory factory =
179                                 DocumentBuilderFactory.newInstance();
180                         DocumentBuilder builder = factory.newDocumentBuilder();
181                         Document document = builder.newDocument();
182
183                         Node root = document.createElement("obfuscator"); // $NON-NLS-1$ //$NON-NLS-1$
184                         document.appendChild(root);
185                         Iterator iter = fIdentifierMap.keySet().iterator();
186                         while (iter.hasNext()) {
187                                 //                      for (int i= 0; i != fTemplates.size(); i++) {
188                                 //                              Template template= (Template) fTemplates.get(i);
189
190                                 Node node = document.createElement("ignore"); // $NON-NLS-1$ //$NON-NLS-1$
191                                 root.appendChild(node);
192
193                                 //                              NamedNodeMap attributes= node.getAttributes();
194                                 //                              
195                                 //                              Attr name= document.createAttribute(NAME_ATTRIBUTE);
196                                 //                              name.setValue(template.getName());
197                                 //                              attributes.setNamedItem(name);
198                                 //      
199                                 //                              Attr description= document.createAttribute(DESCRIPTION_ATTRIBUTE);
200                                 //                              description.setValue(template.getDescription());
201                                 //                              attributes.setNamedItem(description);
202                                 //      
203                                 //                              Attr context= document.createAttribute(CONTEXT_ATTRIBUTE);
204                                 //                              context.setValue(template.getContextTypeName());
205                                 //                              attributes.setNamedItem(context);                       
206                                 //
207                                 //                              Attr enabled= document.createAttribute(ENABLED_ATTRIBUTE);
208                                 //                              enabled.setValue(template.isEnabled() ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$
209                                 //                              attributes.setNamedItem(enabled);
210
211                                 Text pattern = document.createTextNode((String) iter.next());
212                                 node.appendChild(pattern);
213                         }
214
215                         OutputFormat format = new OutputFormat();
216                         format.setPreserveSpace(true);
217                         Serializer serializer = SerializerFactory.getSerializerFactory("xml").makeSerializer(stream, format); //$NON-NLS-1$
218                         serializer.asDOMSerializer().serialize(document);
219
220                 } catch (ParserConfigurationException e) {
221                         throwWriteException(e);
222                 } catch (IOException e) {
223                         throwWriteException(e);
224                 }
225         }
226
227         private static void throwReadException(Throwable t) throws CoreException {
228                 PHPeclipsePlugin.log(t);
229                 //              IStatus status= new JavaUIStatus(JavaStatusConstants.TEMPLATE_IO_EXCEPTION,
230                 //                      ObfuscatorMessages.getString("TemplateSet.error.read"), t); //$NON-NLS-1$
231                 //              throw new JavaUIException(status);
232         }
233
234         private static void throwWriteException(Throwable t) throws CoreException {
235                 PHPeclipsePlugin.log(t);
236                 //              IStatus status= new JavaUIStatus(JavaStatusConstants.TEMPLATE_IO_EXCEPTION,
237                 //                      ObfuscatorMessages.getString("TemplateSet.error.write"), t); //$NON-NLS-1$
238                 //              throw new JavaUIException(status);
239         }
240
241         /**
242          * Adds a template to the set.
243          */
244         //      public void add(Template template) {
245         //              if (exists(template))
246         //                      return; // ignore duplicate
247         //              
248         //              fTemplates.add(template);
249         //              sort();
250         //      }
251
252         //      private boolean exists(Template template) {
253         //              for (Iterator iterator = fTemplates.iterator(); iterator.hasNext();) {
254         //                      Template anotherTemplate = (Template) iterator.next();
255         //
256         //                      if (template.equals(anotherTemplate))
257         //                              return true;
258         //              }
259         //              
260         //              return false;
261         //      }
262         //      
263         //      /**
264         //       * Removes a template to the set.
265         //       */     
266         //      public void remove(Template template) {
267         //              fTemplates.remove(template);
268         //              sort();
269         //      }
270         //
271                 /**
272                  * Empties the set.
273                  */             
274                 public void clear() {
275                         fIdentifierMap.clear();
276         //              fTemplates.clear();
277         //              sort();
278                 }
279         //      
280         //      /**
281         //       * Returns all templates.
282         //       */
283         //      public Template[] getTemplates() {
284         //              return (Template[]) fTemplates.toArray(new Template[fTemplates.size()]);
285         //      }
286
287         /**
288          * Returns all templates with a given name.
289          */
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)) {
295         //                              res.add(curr);
296         //                      }
297         //              }
298         //              return (Template[]) res.toArray(new Template[res.size()]);
299         //      }       
300         //      
301         //      private void sort() {
302         //              fSortedTemplates= (Template[]) fTemplates.toArray(new Template[fTemplates.size()]);
303         //              Arrays.sort(fSortedTemplates, fTemplateComparator);
304         //      }
305
306         /**
307          * @return
308          */
309         public HashMap getIdentifierMap() {
310                 return fIdentifierMap;
311         }
312
313 }