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;
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;
39 * <code>ObfuscatorIgnoreSet</code> manages a collection of templates and makes them
42 public class ObfuscatorIgnoreSet {
44 // private static class TemplateComparator implements Comparator {
45 // public int compare(Object arg0, Object arg1) {
52 // Template template0= (Template) arg0;
53 // Template template1= (Template) arg1;
55 // return template0.getName().compareTo(template1.getName());
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$
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];
71 * Convenience method for reading templates from a file.
73 * @see #addFromStream(InputStream)
75 public void addFromFile(File file) throws CoreException {
76 InputStream stream = null;
79 stream = new FileInputStream(file);
80 addFromStream(stream);
82 } catch (IOException e) {
83 throwReadException(e);
89 } catch (IOException e) {
95 * Reads templates from a XML stream and adds them to the template set.
97 public void addFromStream(InputStream stream) throws CoreException {
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);
105 int count = elements.getLength();
106 for (int i = 0; i != count; i++) {
107 Node node = elements.item(i);
108 NamedNodeMap attributes = node.getAttributes();
110 if (attributes == null)
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);
118 // if (name == null || description == null || context == null)
119 // throw new SAXException(ObfuscatorMessages.getString("TemplateSet.error.missing.attribute")); //$NON-NLS-1$
121 // boolean enabled= true; //(enabledNode == null) || (enabledNode.getNodeValue().equals("true")); //$NON-NLS-1$
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();
128 buffer.append(value);
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);
139 } catch (ParserConfigurationException e) {
140 throwReadException(e);
141 } catch (IOException e) {
142 throwReadException(e);
143 } catch (SAXException e) {
144 throwReadException(e);
148 private String getAttributeValue(NamedNodeMap attributes, String name) {
149 Node node = attributes.getNamedItem(name);
151 return node == null ? null : node.getNodeValue();
155 * Convenience method for saving to a file.
157 * @see #saveToStream(OutputStream)
159 public void saveToFile(File file) throws CoreException {
160 OutputStream stream = null;
163 stream = new FileOutputStream(file);
164 saveToStream(stream);
165 } catch (IOException e) {
166 throwWriteException(e);
172 } catch (IOException e) {
178 * Saves the template set as XML.
180 public void saveToStream(OutputStream stream) throws CoreException {
182 DocumentBuilderFactory factory =
183 DocumentBuilderFactory.newInstance();
184 DocumentBuilder builder = factory.newDocumentBuilder();
185 Document document = builder.newDocument();
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);
194 Node node = document.createElement("ignore"); // $NON-NLS-1$ //$NON-NLS-1$
195 root.appendChild(node);
197 // NamedNodeMap attributes= node.getAttributes();
199 // Attr name= document.createAttribute(NAME_ATTRIBUTE);
200 // name.setValue(template.getName());
201 // attributes.setNamedItem(name);
203 // Attr description= document.createAttribute(DESCRIPTION_ATTRIBUTE);
204 // description.setValue(template.getDescription());
205 // attributes.setNamedItem(description);
207 // Attr context= document.createAttribute(CONTEXT_ATTRIBUTE);
208 // context.setValue(template.getContextTypeName());
209 // attributes.setNamedItem(context);
211 // Attr enabled= document.createAttribute(ENABLED_ATTRIBUTE);
212 // enabled.setValue(template.isEnabled() ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$
213 // attributes.setNamedItem(enabled);
215 Text pattern = document.createTextNode((String) iter.next());
216 node.appendChild(pattern);
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);
224 transformer.transform(source, result);
226 } catch (ParserConfigurationException e) {
227 throwWriteException(e);
228 } catch (TransformerException e) {
229 throwWriteException(e);
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);
236 // } catch (ParserConfigurationException e) {
237 // throwWriteException(e);
238 // } catch (IOException e) {
239 // throwWriteException(e);
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);
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);
258 * Adds a template to the set.
260 // public void add(Template template) {
261 // if (exists(template))
262 // return; // ignore duplicate
264 // fTemplates.add(template);
268 // private boolean exists(Template template) {
269 // for (Iterator iterator = fTemplates.iterator(); iterator.hasNext();) {
270 // Template anotherTemplate = (Template) iterator.next();
272 // if (template.equals(anotherTemplate))
280 // * Removes a template to the set.
282 // public void remove(Template template) {
283 // fTemplates.remove(template);
290 public void clear() {
291 fIdentifierMap.clear();
292 // fTemplates.clear();
297 // * Returns all templates.
299 // public Template[] getTemplates() {
300 // return (Template[]) fTemplates.toArray(new Template[fTemplates.size()]);
304 * Returns all templates with a given name.
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)) {
314 // return (Template[]) res.toArray(new Template[res.size()]);
317 // private void sort() {
318 // fSortedTemplates= (Template[]) fTemplates.toArray(new Template[fTemplates.size()]);
319 // Arrays.sort(fSortedTemplates, fTemplateComparator);
325 public HashMap getIdentifierMap() {
326 return fIdentifierMap;