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