2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpeclipse.mover.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;
20 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
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;
35 * <code>ObfuscatorIgnoreSet</code> manages a collection of templates and makes them
38 public class ObfuscatorIgnoreSet {
40 // private static class TemplateComparator implements Comparator {
41 // public int compare(Object arg0, Object arg1) {
48 // Template template0= (Template) arg0;
49 // Template template1= (Template) arg1;
51 // return template0.getName().compareTo(template1.getName());
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$
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];
67 * Convenience method for reading templates from a file.
69 * @see #addFromStream(InputStream)
71 public void addFromFile(File file) throws CoreException {
72 InputStream stream = null;
75 stream = new FileInputStream(file);
76 addFromStream(stream);
78 } catch (IOException e) {
79 throwReadException(e);
85 } catch (IOException e) {
91 * Reads templates from a XML stream and adds them to the template set.
93 public void addFromStream(InputStream stream) throws CoreException {
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);
101 int count = elements.getLength();
102 for (int i = 0; i != count; i++) {
103 Node node = elements.item(i);
104 NamedNodeMap attributes = node.getAttributes();
106 if (attributes == null)
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);
114 // if (name == null || description == null || context == null)
115 // throw new SAXException(ObfuscatorMessages.getString("TemplateSet.error.missing.attribute")); //$NON-NLS-1$
117 // boolean enabled= true; //(enabledNode == null) || (enabledNode.getNodeValue().equals("true")); //$NON-NLS-1$
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();
124 buffer.append(value);
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);
135 } catch (ParserConfigurationException e) {
136 throwReadException(e);
137 } catch (IOException e) {
138 throwReadException(e);
139 } catch (SAXException e) {
140 throwReadException(e);
144 private String getAttributeValue(NamedNodeMap attributes, String name) {
145 Node node = attributes.getNamedItem(name);
147 return node == null ? null : node.getNodeValue();
151 * Convenience method for saving to a file.
153 * @see #saveToStream(OutputStream)
155 public void saveToFile(File file) throws CoreException {
156 OutputStream stream = null;
159 stream = new FileOutputStream(file);
160 saveToStream(stream);
161 } catch (IOException e) {
162 throwWriteException(e);
168 } catch (IOException e) {
174 * Saves the template set as XML.
176 public void saveToStream(OutputStream stream) throws CoreException {
178 DocumentBuilderFactory factory =
179 DocumentBuilderFactory.newInstance();
180 DocumentBuilder builder = factory.newDocumentBuilder();
181 Document document = builder.newDocument();
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);
190 Node node = document.createElement("ignore"); // $NON-NLS-1$ //$NON-NLS-1$
191 root.appendChild(node);
193 // NamedNodeMap attributes= node.getAttributes();
195 // Attr name= document.createAttribute(NAME_ATTRIBUTE);
196 // name.setValue(template.getName());
197 // attributes.setNamedItem(name);
199 // Attr description= document.createAttribute(DESCRIPTION_ATTRIBUTE);
200 // description.setValue(template.getDescription());
201 // attributes.setNamedItem(description);
203 // Attr context= document.createAttribute(CONTEXT_ATTRIBUTE);
204 // context.setValue(template.getContextTypeName());
205 // attributes.setNamedItem(context);
207 // Attr enabled= document.createAttribute(ENABLED_ATTRIBUTE);
208 // enabled.setValue(template.isEnabled() ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$
209 // attributes.setNamedItem(enabled);
211 Text pattern = document.createTextNode((String) iter.next());
212 node.appendChild(pattern);
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);
220 } catch (ParserConfigurationException e) {
221 throwWriteException(e);
222 } catch (IOException e) {
223 throwWriteException(e);
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);
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);
242 * Adds a template to the set.
244 // public void add(Template template) {
245 // if (exists(template))
246 // return; // ignore duplicate
248 // fTemplates.add(template);
252 // private boolean exists(Template template) {
253 // for (Iterator iterator = fTemplates.iterator(); iterator.hasNext();) {
254 // Template anotherTemplate = (Template) iterator.next();
256 // if (template.equals(anotherTemplate))
264 // * Removes a template to the set.
266 // public void remove(Template template) {
267 // fTemplates.remove(template);
274 public void clear() {
275 fIdentifierMap.clear();
276 // fTemplates.clear();
281 // * Returns all templates.
283 // public Template[] getTemplates() {
284 // return (Template[]) fTemplates.toArray(new Template[fTemplates.size()]);
288 * Returns all templates with a given name.
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)) {
298 // return (Template[]) res.toArray(new Template[res.size()]);
301 // private void sort() {
302 // fSortedTemplates= (Template[]) fTemplates.toArray(new Template[fTemplates.size()]);
303 // Arrays.sort(fSortedTemplates, fTemplateComparator);
309 public HashMap getIdentifierMap() {
310 return fIdentifierMap;