0fa841317c8e9d7671b5168b23448937bd8bf27b
[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 import javax.xml.transform.OutputKeys;
20 import javax.xml.transform.Transformer;
21 import javax.xml.transform.TransformerException;
22 import javax.xml.transform.TransformerFactory;
23 import javax.xml.transform.dom.DOMSource;
24 import javax.xml.transform.stream.StreamResult;
25
26 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
27
28
29 import org.eclipse.core.runtime.CoreException;
30 import org.w3c.dom.Document;
31 import org.w3c.dom.NamedNodeMap;
32 import org.w3c.dom.Node;
33 import org.w3c.dom.NodeList;
34 import org.w3c.dom.Text;
35 import org.xml.sax.InputSource;
36 import org.xml.sax.SAXException;
37
38 /**
39  * <code>ObfuscatorIgnoreSet</code> manages a collection of templates and makes them
40  * persistent.
41  */
42 public class ObfuscatorIgnoreSet {
43
44         //      private static class TemplateComparator implements Comparator {
45         //              public int compare(Object arg0, Object arg1) {
46         //                      if (arg0 == arg1)
47         //                              return 0;
48         //                      
49         //                      if (arg0 == null)
50         //                              return -1;
51         //                              
52         //                      Template template0= (Template) arg0;
53         //                      Template template1= (Template) arg1;
54         //                      
55         //                      return template0.getName().compareTo(template1.getName());
56         //              }
57         //      }
58
59         private static final String TEMPLATE_TAG = "ignore"; //$NON-NLS-1$
60         //      private static final String NAME_ATTRIBUTE= "name"; //$NON-NLS-1$
61         //      private static final String DESCRIPTION_ATTRIBUTE= "description"; //$NON-NLS-1$
62         //      private static final String CONTEXT_ATTRIBUTE= "context"; //$NON-NLS-1$
63         //      private static final String ENABLED_ATTRIBUTE= "enabled"; //$NON-NLS-1$
64
65         //      private List fTemplates= new ArrayList();
66         private HashMap fIdentifierMap = new HashMap();
67         //      private Comparator fTemplateComparator= new TemplateComparator();
68         //      private Template[] fSortedTemplates= new Template[0];
69
70         /**
71          * Convenience method for reading templates from a file.
72          * 
73          * @see #addFromStream(InputStream)
74          */
75         public void addFromFile(File file) throws CoreException {
76                 InputStream stream = null;
77
78                 try {
79                         stream = new FileInputStream(file);
80                         addFromStream(stream);
81
82                 } catch (IOException e) {
83                         throwReadException(e);
84
85                 } finally {
86                         try {
87                                 if (stream != null)
88                                         stream.close();
89                         } catch (IOException e) {
90                         }
91                 }
92         }
93
94         /**
95          * Reads templates from a XML stream and adds them to the template set.
96          */
97         public void addFromStream(InputStream stream) throws CoreException {
98                 try {
99                         DocumentBuilderFactory factory =
100                                 DocumentBuilderFactory.newInstance();
101                         DocumentBuilder parser = factory.newDocumentBuilder();
102                         Document document = parser.parse(new InputSource(stream));
103                         NodeList elements = document.getElementsByTagName(TEMPLATE_TAG);
104
105                         int count = elements.getLength();
106                         for (int i = 0; i != count; i++) {
107                                 Node node = elements.item(i);
108                                 NamedNodeMap attributes = node.getAttributes();
109
110                                 if (attributes == null)
111                                         continue;
112
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);
117
118                                 //                              if (name == null || description == null || context == null)
119                                 //                                      throw new SAXException(ObfuscatorMessages.getString("TemplateSet.error.missing.attribute")); //$NON-NLS-1$
120
121                                 //                              boolean enabled= true; //(enabledNode == null) || (enabledNode.getNodeValue().equals("true")); //$NON-NLS-1$
122
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();
127                                         if (value != null)
128                                                 buffer.append(value);
129                                 }
130                                 String pattern = buffer.toString().trim();
131                                 fIdentifierMap.put(pattern, new PHPIdentifier(pattern, PHPIdentifier.VARIABLE) );
132                                 //                              Template template= new Template(name, description, context, pattern);   
133                                 //                              template.setEnabled(enabled);
134                                 //                              add(template);
135                         }
136
137                         //                      sort();
138
139                 } catch (ParserConfigurationException e) {
140                         throwReadException(e);
141                 } catch (IOException e) {
142                         throwReadException(e);
143                 } catch (SAXException e) {
144                         throwReadException(e);
145                 }
146         }
147
148         private String getAttributeValue(NamedNodeMap attributes, String name) {
149                 Node node = attributes.getNamedItem(name);
150
151                 return node == null ? null : node.getNodeValue();
152         }
153
154         /**
155          * Convenience method for saving to a file.
156          * 
157          * @see #saveToStream(OutputStream)
158          */
159         public void saveToFile(File file) throws CoreException {
160                 OutputStream stream = null;
161
162                 try {
163                         stream = new FileOutputStream(file);
164                         saveToStream(stream);
165                 } catch (IOException e) {
166                         throwWriteException(e);
167
168                 } finally {
169                         try {
170                                 if (stream != null)
171                                         stream.close();
172                         } catch (IOException e) {
173                         }
174                 }
175         }
176
177         /**
178          * Saves the template set as XML.
179          */
180         public void saveToStream(OutputStream stream) throws CoreException {
181                 try {
182                         DocumentBuilderFactory factory =
183                                 DocumentBuilderFactory.newInstance();
184                         DocumentBuilder builder = factory.newDocumentBuilder();
185                         Document document = builder.newDocument();
186
187                         Node root = document.createElement("obfuscator"); // $NON-NLS-1$ //$NON-NLS-1$
188                         document.appendChild(root);
189                         Iterator iter = fIdentifierMap.keySet().iterator();
190                         while (iter.hasNext()) {
191                                 //                      for (int i= 0; i != fTemplates.size(); i++) {
192                                 //                              Template template= (Template) fTemplates.get(i);
193
194                                 Node node = document.createElement("ignore"); // $NON-NLS-1$ //$NON-NLS-1$
195                                 root.appendChild(node);
196
197                                 //                              NamedNodeMap attributes= node.getAttributes();
198                                 //                              
199                                 //                              Attr name= document.createAttribute(NAME_ATTRIBUTE);
200                                 //                              name.setValue(template.getName());
201                                 //                              attributes.setNamedItem(name);
202                                 //      
203                                 //                              Attr description= document.createAttribute(DESCRIPTION_ATTRIBUTE);
204                                 //                              description.setValue(template.getDescription());
205                                 //                              attributes.setNamedItem(description);
206                                 //      
207                                 //                              Attr context= document.createAttribute(CONTEXT_ATTRIBUTE);
208                                 //                              context.setValue(template.getContextTypeName());
209                                 //                              attributes.setNamedItem(context);                       
210                                 //
211                                 //                              Attr enabled= document.createAttribute(ENABLED_ATTRIBUTE);
212                                 //                              enabled.setValue(template.isEnabled() ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$
213                                 //                              attributes.setNamedItem(enabled);
214
215                                 Text pattern = document.createTextNode((String) iter.next());
216                                 node.appendChild(pattern);
217                         }
218                         Transformer transformer=TransformerFactory.newInstance().newTransformer();
219                         transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
220                         transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //$NON-NLS-1$
221                         DOMSource source = new DOMSource(document);
222                         StreamResult result = new StreamResult(stream);
223
224                         transformer.transform(source, result);
225
226                 } catch (ParserConfigurationException e) {
227                         throwWriteException(e);
228                 } catch (TransformerException e) {
229                         throwWriteException(e);
230                 }               
231 //                      OutputFormat format = new OutputFormat();
232 //                      format.setPreserveSpace(true);
233 //                      Serializer serializer = SerializerFactory.getSerializerFactory("xml").makeSerializer(stream, format); //$NON-NLS-1$
234 //                      serializer.asDOMSerializer().serialize(document);
235 //
236 //              } catch (ParserConfigurationException e) {
237 //                      throwWriteException(e);
238 //              } catch (IOException e) {
239 //                      throwWriteException(e);
240 //              }
241         }
242
243         private static void throwReadException(Throwable t) throws CoreException {
244                 PHPeclipsePlugin.log(t);
245                 //              IStatus status= new JavaUIStatus(JavaStatusConstants.TEMPLATE_IO_EXCEPTION,
246                 //                      ObfuscatorMessages.getString("TemplateSet.error.read"), t); //$NON-NLS-1$
247                 //              throw new JavaUIException(status);
248         }
249
250         private static void throwWriteException(Throwable t) throws CoreException {
251                 PHPeclipsePlugin.log(t);
252                 //              IStatus status= new JavaUIStatus(JavaStatusConstants.TEMPLATE_IO_EXCEPTION,
253                 //                      ObfuscatorMessages.getString("TemplateSet.error.write"), t); //$NON-NLS-1$
254                 //              throw new JavaUIException(status);
255         }
256
257         /**
258          * Adds a template to the set.
259          */
260         //      public void add(Template template) {
261         //              if (exists(template))
262         //                      return; // ignore duplicate
263         //              
264         //              fTemplates.add(template);
265         //              sort();
266         //      }
267
268         //      private boolean exists(Template template) {
269         //              for (Iterator iterator = fTemplates.iterator(); iterator.hasNext();) {
270         //                      Template anotherTemplate = (Template) iterator.next();
271         //
272         //                      if (template.equals(anotherTemplate))
273         //                              return true;
274         //              }
275         //              
276         //              return false;
277         //      }
278         //      
279         //      /**
280         //       * Removes a template to the set.
281         //       */     
282         //      public void remove(Template template) {
283         //              fTemplates.remove(template);
284         //              sort();
285         //      }
286         //
287                 /**
288                  * Empties the set.
289                  */             
290                 public void clear() {
291                         fIdentifierMap.clear();
292         //              fTemplates.clear();
293         //              sort();
294                 }
295         //      
296         //      /**
297         //       * Returns all templates.
298         //       */
299         //      public Template[] getTemplates() {
300         //              return (Template[]) fTemplates.toArray(new Template[fTemplates.size()]);
301         //      }
302
303         /**
304          * Returns all templates with a given name.
305          */
306         //      public Template[] getTemplates(String name) {
307         //              ArrayList res= new ArrayList();
308         //              for (Iterator iterator= fTemplates.iterator(); iterator.hasNext();) {
309         //                      Template curr= (Template) iterator.next();
310         //                      if (curr.getName().equals(name)) {
311         //                              res.add(curr);
312         //                      }
313         //              }
314         //              return (Template[]) res.toArray(new Template[res.size()]);
315         //      }       
316         //      
317         //      private void sort() {
318         //              fSortedTemplates= (Template[]) fTemplates.toArray(new Template[fTemplates.size()]);
319         //              Arrays.sort(fSortedTemplates, fTemplateComparator);
320         //      }
321
322         /**
323          * @return
324          */
325         public HashMap getIdentifierMap() {
326                 return fIdentifierMap;
327         }
328
329 }