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