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 makes them
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$
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$
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];
70 * Convenience method for reading templates from a file.
72 * @see #addFromStream(InputStream)
74 public void addFromFile(File file) throws CoreException {
75 InputStream stream = null;
78 stream = new FileInputStream(file);
79 addFromStream(stream);
81 } catch (IOException e) {
82 throwReadException(e);
88 } catch (IOException e) {
94 * Reads templates from a XML stream and adds them to the template set.
96 public void addFromStream(InputStream stream) throws CoreException {
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);
104 int count = elements.getLength();
105 for (int i = 0; i != count; i++) {
106 Node node = elements.item(i);
107 NamedNodeMap attributes = node.getAttributes();
109 if (attributes == null)
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);
117 // if (name == null || description == null || context == null)
118 // throw new SAXException(ObfuscatorMessages.getString("TemplateSet.error.missing.attribute")); //$NON-NLS-1$
120 // boolean enabled= true; //(enabledNode == null) || (enabledNode.getNodeValue().equals("true")); //$NON-NLS-1$
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();
127 buffer.append(value);
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);
138 } catch (ParserConfigurationException e) {
139 throwReadException(e);
140 } catch (IOException e) {
141 throwReadException(e);
142 } catch (SAXException e) {
143 throwReadException(e);
147 private String getAttributeValue(NamedNodeMap attributes, String name) {
148 Node node = attributes.getNamedItem(name);
150 return node == null ? null : node.getNodeValue();
154 * Convenience method for saving to a file.
156 * @see #saveToStream(OutputStream)
158 public void saveToFile(File file) throws CoreException {
159 OutputStream stream = null;
162 stream = new FileOutputStream(file);
163 saveToStream(stream);
164 } catch (IOException e) {
165 throwWriteException(e);
171 } catch (IOException e) {
177 * Saves the template set as XML.
179 public void saveToStream(OutputStream stream) throws CoreException {
181 DocumentBuilderFactory factory =
182 DocumentBuilderFactory.newInstance();
183 DocumentBuilder builder = factory.newDocumentBuilder();
184 Document document = builder.newDocument();
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);
193 Node node = document.createElement("ignore"); // $NON-NLS-1$ //$NON-NLS-1$
194 root.appendChild(node);
196 // NamedNodeMap attributes= node.getAttributes();
198 // Attr name= document.createAttribute(NAME_ATTRIBUTE);
199 // name.setValue(template.getName());
200 // attributes.setNamedItem(name);
202 // Attr description= document.createAttribute(DESCRIPTION_ATTRIBUTE);
203 // description.setValue(template.getDescription());
204 // attributes.setNamedItem(description);
206 // Attr context= document.createAttribute(CONTEXT_ATTRIBUTE);
207 // context.setValue(template.getContextTypeName());
208 // attributes.setNamedItem(context);
210 // Attr enabled= document.createAttribute(ENABLED_ATTRIBUTE);
211 // enabled.setValue(template.isEnabled() ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$
212 // attributes.setNamedItem(enabled);
214 Text pattern = document.createTextNode((String) iter.next());
215 node.appendChild(pattern);
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);
223 transformer.transform(source, result);
225 } catch (ParserConfigurationException e) {
226 throwWriteException(e);
227 } catch (TransformerException e) {
228 throwWriteException(e);
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);
235 // } catch (ParserConfigurationException e) {
236 // throwWriteException(e);
237 // } catch (IOException e) {
238 // throwWriteException(e);
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);
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);
257 * Adds a template to the set.
259 // public void add(Template template) {
260 // if (exists(template))
261 // return; // ignore duplicate
263 // fTemplates.add(template);
267 // private boolean exists(Template template) {
268 // for (Iterator iterator = fTemplates.iterator(); iterator.hasNext();) {
269 // Template anotherTemplate = (Template) iterator.next();
271 // if (template.equals(anotherTemplate))
279 // * Removes a template to the set.
281 // public void remove(Template template) {
282 // fTemplates.remove(template);
289 public void clear() {
290 fIdentifierMap.clear();
291 // fTemplates.clear();
296 // * Returns all templates.
298 // public Template[] getTemplates() {
299 // return (Template[]) fTemplates.toArray(new Template[fTemplates.size()]);
303 * Returns all templates with a given name.
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)) {
313 // return (Template[]) res.toArray(new Template[res.size()]);
316 // private void sort() {
317 // fSortedTemplates= (Template[]) fTemplates.toArray(new Template[fTemplates.size()]);
318 // Arrays.sort(fSortedTemplates, fTemplateComparator);
324 public HashMap getIdentifierMap() {
325 return fIdentifierMap;