2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpeclipse.obfuscator;
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;
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;
26 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
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;
38 * <code>ObfuscatorIgnoreSet</code> manages a collection of templates and
39 * makes them persistent.
41 public class ObfuscatorIgnoreSet {
43 // private static class TemplateComparator implements Comparator {
44 // public int compare(Object arg0, Object arg1) {
51 // Template template0= (Template) arg0;
52 // Template template1= (Template) arg1;
54 // return template0.getName().compareTo(template1.getName());
58 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";
63 // private static final String CONTEXT_ATTRIBUTE= "context"; //$NON-NLS-1$
64 // private static final String ENABLED_ATTRIBUTE= "enabled"; //$NON-NLS-1$
66 // private List fTemplates= new ArrayList();
67 private HashMap fIdentifierMap = new HashMap();
69 // private Comparator fTemplateComparator= new TemplateComparator();
70 // private Template[] fSortedTemplates= new Template[0];
73 * Convenience method for reading templates from a file.
75 * @see #addFromStream(InputStream)
77 public void addFromFile(File file) throws CoreException {
78 InputStream stream = null;
81 stream = new FileInputStream(file);
82 addFromStream(stream);
84 } catch (IOException e) {
85 throwReadException(e);
91 } catch (IOException e) {
97 * Reads templates from a XML stream and adds them to the template set.
99 public void addFromStream(InputStream stream) throws CoreException {
101 DocumentBuilderFactory factory = DocumentBuilderFactory
103 DocumentBuilder parser = factory.newDocumentBuilder();
104 Document document = parser.parse(new InputSource(stream));
105 NodeList elements = document.getElementsByTagName(TEMPLATE_TAG);
107 int count = elements.getLength();
108 for (int i = 0; i != count; i++) {
109 Node node = elements.item(i);
110 NamedNodeMap attributes = node.getAttributes();
112 if (attributes == null)
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);
122 // if (name == null || description == null || context == null)
124 // SAXException(ObfuscatorMessages.getString("TemplateSet.error.missing.attribute"));
127 // boolean enabled= true; //(enabledNode == null) ||
128 // (enabledNode.getNodeValue().equals("true")); //$NON-NLS-1$
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();
135 buffer.append(value);
137 String pattern = buffer.toString().trim();
138 fIdentifierMap.put(pattern, new PHPIdentifier(pattern,
139 PHPIdentifier.VARIABLE));
140 // Template template= new Template(name, description, context,
142 // template.setEnabled(enabled);
148 } catch (ParserConfigurationException e) {
149 throwReadException(e);
150 } catch (IOException e) {
151 throwReadException(e);
152 } catch (SAXException e) {
153 throwReadException(e);
157 private String getAttributeValue(NamedNodeMap attributes, String name) {
158 Node node = attributes.getNamedItem(name);
160 return node == null ? null : node.getNodeValue();
164 * Convenience method for saving to a file.
166 * @see #saveToStream(OutputStream)
168 public void saveToFile(File file) throws CoreException {
169 OutputStream stream = null;
172 stream = new FileOutputStream(file);
173 saveToStream(stream);
174 } catch (IOException e) {
175 throwWriteException(e);
181 } catch (IOException e) {
187 * Saves the template set as XML.
189 public void saveToStream(OutputStream stream) throws CoreException {
191 DocumentBuilderFactory factory = DocumentBuilderFactory
193 DocumentBuilder builder = factory.newDocumentBuilder();
194 Document document = builder.newDocument();
196 Node root = document.createElement("obfuscator"); // $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);
204 Node node = document.createElement("ignore"); // $NON-NLS-1$
206 root.appendChild(node);
208 // NamedNodeMap attributes= node.getAttributes();
210 // Attr name= document.createAttribute(NAME_ATTRIBUTE);
211 // name.setValue(template.getName());
212 // attributes.setNamedItem(name);
215 // document.createAttribute(DESCRIPTION_ATTRIBUTE);
216 // description.setValue(template.getDescription());
217 // attributes.setNamedItem(description);
219 // Attr context= document.createAttribute(CONTEXT_ATTRIBUTE);
220 // context.setValue(template.getContextTypeName());
221 // attributes.setNamedItem(context);
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);
228 Text pattern = document.createTextNode((String) iter.next());
229 node.appendChild(pattern);
231 Transformer transformer = TransformerFactory.newInstance()
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);
238 transformer.transform(source, result);
240 } catch (ParserConfigurationException e) {
241 throwWriteException(e);
242 } catch (TransformerException e) {
243 throwWriteException(e);
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);
252 // } catch (ParserConfigurationException e) {
253 // throwWriteException(e);
254 // } catch (IOException e) {
255 // throwWriteException(e);
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);
265 // throw new JavaUIException(status);
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);
274 // throw new JavaUIException(status);
278 * Adds a template to the set.
280 // public void add(Template template) {
281 // if (exists(template))
282 // return; // ignore duplicate
284 // fTemplates.add(template);
287 // private boolean exists(Template template) {
288 // for (Iterator iterator = fTemplates.iterator(); iterator.hasNext();) {
289 // Template anotherTemplate = (Template) iterator.next();
291 // if (template.equals(anotherTemplate))
299 // * Removes a template to the set.
301 // public void remove(Template template) {
302 // fTemplates.remove(template);
309 public void clear() {
310 fIdentifierMap.clear();
311 // fTemplates.clear();
317 // * Returns all templates.
319 // public Template[] getTemplates() {
320 // return (Template[]) fTemplates.toArray(new Template[fTemplates.size()]);
324 * Returns all templates with a given name.
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)) {
334 // return (Template[]) res.toArray(new Template[res.size()]);
337 // private void sort() {
338 // fSortedTemplates= (Template[]) fTemplates.toArray(new
339 // Template[fTemplates.size()]);
340 // Arrays.sort(fSortedTemplates, fTemplateComparator);
345 public HashMap getIdentifierMap() {
346 return fIdentifierMap;