replace deprecated org.eclipse.jface.util.ListenerList
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / phpeditor / PHPSyntaxRdr.java
1 package net.sourceforge.phpeclipse.phpeditor;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.OutputStream;
9 import java.util.ArrayList;
10
11 import javax.xml.parsers.DocumentBuilder;
12 import javax.xml.parsers.DocumentBuilderFactory;
13 import javax.xml.parsers.ParserConfigurationException;
14 import javax.xml.transform.OutputKeys;
15 import javax.xml.transform.Transformer;
16 import javax.xml.transform.TransformerException;
17 import javax.xml.transform.TransformerFactory;
18 import javax.xml.transform.dom.DOMSource;
19 import javax.xml.transform.stream.StreamResult;
20
21 import net.sourceforge.phpeclipse.IPreferenceConstants;
22 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
23 import net.sourceforge.phpeclipse.phpeditor.php.PHPConstant;
24 import net.sourceforge.phpeclipse.phpeditor.php.PHPElement;
25 import net.sourceforge.phpeclipse.phpeditor.php.PHPFunction;
26 import net.sourceforge.phpeclipse.phpeditor.php.PHPKeyword;
27 import net.sourceforge.phpeclipse.phpeditor.php.PHPType;
28 import net.sourceforge.phpeclipse.ui.WebUI;
29
30 import org.eclipse.core.runtime.CoreException;
31 import org.eclipse.core.runtime.IPath;
32 import org.eclipse.jface.preference.IPreferenceStore;
33 import org.w3c.dom.Attr;
34 import org.w3c.dom.Document;
35 import org.w3c.dom.NamedNodeMap;
36 import org.w3c.dom.Node;
37 import org.w3c.dom.NodeList;
38 import org.w3c.dom.Text;
39 import org.xml.sax.InputSource;
40 import org.xml.sax.SAXException;
41 import org.xml.sax.SAXParseException;
42
43 /**
44  * <code>PHPSyntaxRdr</code> reads PHP specifics from an XML file (eg.
45  * keywords)
46  */
47
48 public class PHPSyntaxRdr {
49         // private static final String PHPDEFAULT_FILE = "default-syntax.xml";
50         // //$NON-NLS-1$
51         private static final String PHPSYNTAX_FILE = "syntax.xml"; //$NON-NLS-1$
52         // private static final String USERSYNTAX_FILE = "usersyntax.xml";
53         // //$NON-NLS-1$
54         // private static final String USERDEFAULT_FILE = "default-usersyntax.xml";
55         // //$NON-NLS-1$
56
57         private static final String PHPSYNTAX_TAG = "s"; //$NON-NLS-1$
58
59         private static final String KEYWORD_ATTR = "k"; //$NON-NLS-1$
60
61         private static final String TYPE_ATTR = "t"; //$NON-NLS-1$
62
63         private static final String CONSTANT_ATTR = "c"; //$NON-NLS-1$
64
65         private static final String FN_ATTR = "f"; //$NON-NLS-1$
66
67         private static final String USAGE_ATTR = "u"; //$NON-NLS-1$
68         // private static final String TOKENVAL_ATTR = "tokenval"; //$NON-NLS-1$
69
70         private static IPreferenceStore store;
71
72         //private static boolean hasXMLFileBeenRead = true;
73
74         // The following variable is used to hold the syntax from
75         // the suers custom file - if that file should be changed,
76         // then all entries in this variable should be removed from
77         // the word list, reread from the file and then reinserted.
78         private static ArrayList userdefsyntaxdata;
79
80         private static ArrayList syntaxdata;
81
82         public PHPSyntaxRdr() {
83                 // see getSyntaxData()
84                 syntaxdata = null;
85                 store = WebUI.getDefault().getPreferenceStore();
86         }
87
88         public static void readInSyntax() {
89                 try {
90 //                      hasXMLFileBeenRead = true;
91                         /*
92                          * Attempt to read the syntax file from the metadata if this does
93                          * not work, create metadata from default
94                          */
95                         /*File syntaxFile = getSyntaxFile();
96                         if (syntaxFile.exists()) {
97                                 readFromFile(syntaxFile);
98                         } else {*/
99                                 readFromStream(PHPSyntaxRdr.class
100                                                 .getResourceAsStream(PHPSYNTAX_FILE));
101                                 //saveToFile(syntaxFile);
102                         /*}*/
103                         /* Read the user-defined syntax file if it exists */
104                         // String buffer = new
105                         // String(store.getString(PHPeclipsePlugin.PHP_USERDEF_XMLFILE));
106                         /*if (store == null)
107                                 store = PHPeclipsePlugin.getDefault().getPreferenceStore();
108                         String buffer = new String(store
109                                         .getString(IPreferenceConstants.PHP_USERDEF_XMLFILE));
110                         if (!(buffer.equals("") || buffer == null)) {
111                                 readFromFile(buffer);
112                         }*/
113                 } catch (CoreException ce) {
114                         ce.printStackTrace();
115                 }
116         }
117
118         public static void readFromFile(String filename) {
119                 try {
120                         readFromFile(new File(filename));
121                 } catch (CoreException e) {
122                 }
123         }
124
125         public static void readFromFile(File file) throws CoreException {
126                 InputStream stream = null;
127
128                 if (file.exists()) {
129                         try {
130                                 stream = new FileInputStream(file);
131                                 readFromStream(stream);
132                         } catch (IOException e) {
133                                 throwReadException(e);
134                         } finally {
135                                 try {
136                                         if (stream != null) {
137                                                 stream.close();
138                                         }
139                                 } catch (IOException e) {
140                                 }
141                         }
142                 }
143         }
144
145         public static void readFromStream(InputStream stream) throws CoreException {
146                 try {
147                         DocumentBuilderFactory factory = DocumentBuilderFactory
148                                         .newInstance();
149                         DocumentBuilder parser = factory.newDocumentBuilder();
150                         Document document = parser.parse(new InputSource(stream));
151                         // Read in the Standard PHPSyntax "stuff"
152                         NodeList elements = document.getElementsByTagName(PHPSYNTAX_TAG);
153
154                         int count = elements.getLength();
155                         for (int i = 0; i != count; i++) {
156                                 Node node = elements.item(i);
157                                 NamedNodeMap attributes = node.getAttributes();
158
159                                 if (attributes == null)
160                                         continue;
161
162                                 String Keyword = getAttributeValue(attributes, KEYWORD_ATTR);
163                                 String Type = getAttributeValue(attributes, TYPE_ATTR);
164                                 String Function = getAttributeValue(attributes, FN_ATTR);
165                                 String Constant = getAttributeValue(attributes, CONSTANT_ATTR);
166                                 String usage = getAttributeValue(attributes, USAGE_ATTR);
167                                 // String Tokenval = getAttributeValue(attributes,
168                                 // TOKENVAL_ATTR);
169
170                                 StringBuffer buffer = new StringBuffer();
171                                 NodeList children = node.getChildNodes();
172                                 for (int j = 0; j != children.getLength(); j++) {
173                                         String value = children.item(j).getNodeValue();
174                                         if (value != null)
175                                                 buffer.append(value);
176                                 }
177                                 String description = buffer.toString().trim();
178
179                                 if (Keyword == null && Type == null && Function == null
180                                                 && Constant == null) {
181                                         // ignore as it is not a valid phpsyntax tag
182                                 } else {
183                                         if (Keyword != null) {
184                                                 // syntaxdata.add(new PHPKeyword(Keyword, usage,
185                                                 // Tokenval));
186                                                 syntaxdata.add(new PHPKeyword(Keyword, usage));
187                                         } else if (Type != null) {
188                                                 syntaxdata.add(new PHPType(Type, usage));
189                                         } else if (Function != null) {
190                                                 syntaxdata.add(new PHPFunction(Function, usage,
191                                                                 description));
192                                         } else if (Constant != null) {
193                                                 syntaxdata.add(new PHPConstant(Constant, null,
194                                                                 description));
195                                         }
196                                 }
197                         }
198                 } catch (ParserConfigurationException e) {
199                         throwReadException(e);
200                 } catch (IOException e) {
201                         throwReadException(e);
202                 } catch (SAXParseException e) {
203                         System.out.println("SAXParseException in line:" + e.getLineNumber()
204                                         + " column:" + e.getColumnNumber());
205                         throwReadException(e);
206                 } catch (SAXException e) {
207                         throwReadException(e);
208                 }
209         }
210
211         public static ArrayList getSyntaxData() {
212                 if (syntaxdata == null) {
213                         syntaxdata = new ArrayList();
214                         readInSyntax();
215                 }
216                 return syntaxdata;
217         }
218
219         public static void replaceUserDefFile() {
220                 /* Replace the user-defined syntax file if it exists */
221                 String buffer = new String(store
222                                 .getString(IPreferenceConstants.PHP_USERDEF_XMLFILE));
223                 if (!buffer.equals("") || buffer == null) {
224                         readFromFile(buffer);
225                 }
226         }
227
228         public static ArrayList getUserSyntaxData() {
229                 return userdefsyntaxdata;
230         }
231
232         private static File getSyntaxFile() {
233                 IPath path = PHPeclipsePlugin.getDefault().getStateLocation();
234                 path = path.append(PHPSYNTAX_FILE);
235                 return path.toFile();
236         }
237
238         private static String getAttributeValue(NamedNodeMap attributes, String name) {
239                 Node node = attributes.getNamedItem(name);
240                 return node == null ? null : node.getNodeValue();
241         }
242
243         public static void saveToFile(File file) throws CoreException {
244                 OutputStream stream = null;
245                 try {
246                         stream = new FileOutputStream(file);
247                         saveToStream(stream);
248                 } catch (IOException e) {
249                         throwWriteException(e);
250                 } finally {
251                         try {
252                                 if (stream != null)
253                                         stream.close();
254                         } catch (IOException e) {
255                         }
256                 }
257         }
258
259         public static void saveToStream(OutputStream stream) throws CoreException {
260                 try {
261                         DocumentBuilderFactory factory = DocumentBuilderFactory
262                                         .newInstance();
263                         DocumentBuilder builder = factory.newDocumentBuilder();
264                         Document document = builder.newDocument();
265                         Node root = document.createElement("PHPStandardSyntax"); // $NON-NLS-1$
266                                                                                                                                                 // //$NON-NLS-1$
267                         document.appendChild(root);
268                         for (int i = 0; i != syntaxdata.size(); i++) {
269                                 Object bufferobj = (Object) syntaxdata.get(i);
270                                 Attr name = null;
271                                 Node node = document.createElement(PHPSYNTAX_TAG); // $NON-NLS-1$
272                                                                                                                                         // //$NON-NLS-1$
273                                 root.appendChild(node);
274                                 NamedNodeMap attributes = node.getAttributes();
275                                 if (bufferobj instanceof PHPType)
276                                         name = document.createAttribute(TYPE_ATTR);
277                                 if (bufferobj instanceof PHPKeyword)
278                                         name = document.createAttribute(KEYWORD_ATTR);
279                                 if (bufferobj instanceof PHPFunction)
280                                         name = document.createAttribute(FN_ATTR);
281                                 if (bufferobj instanceof PHPConstant)
282                                         name = document.createAttribute(CONSTANT_ATTR);
283                                 name.setValue(((PHPElement) bufferobj).getName());
284                                 attributes.setNamedItem(name);
285                                 Attr description = document.createAttribute(USAGE_ATTR);
286                                 description.setValue(((PHPElement) bufferobj).getUsage());
287                                 attributes.setNamedItem(description);
288                                 // if (bufferobj instanceof PHPKeyword) {
289                                 // Attr tokenval = document.createAttribute(TOKENVAL_ATTR);
290                                 // tokenval.setValue((new Integer(((PHPKeyword)
291                                 // bufferobj).gettokenval())).toString());
292                                 // attributes.setNamedItem(tokenval);
293                                 // }
294                                 if (bufferobj instanceof PHPFunction) {
295                                         // Attr usage = document.createAttribute(USAGE_ATTR);
296                                         Text usage = document
297                                                         .createTextNode(((PHPFunction) bufferobj)
298                                                                         .getDescription());
299                                         node.appendChild(usage);
300                                 }
301                                 if (bufferobj instanceof PHPConstant) {
302                                         // Attr usage = document.createAttribute(USAGE_ATTR);
303                                         Text usage = document
304                                                         .createTextNode(((PHPConstant) bufferobj)
305                                                                         .getDescription());
306                                         node.appendChild(usage);
307                                 }
308                         }
309                         Transformer transformer = TransformerFactory.newInstance()
310                                         .newTransformer();
311                         transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
312                         transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //$NON-NLS-1$
313                         DOMSource source = new DOMSource(document);
314                         StreamResult result = new StreamResult(stream);
315
316                         transformer.transform(source, result);
317
318                 } catch (ParserConfigurationException e) {
319                         throwWriteException(e);
320                 } catch (TransformerException e) {
321                         throwWriteException(e);
322                 }
323                 // OutputFormat format = new OutputFormat();
324                 // format.setPreserveSpace(true);
325                 // try {
326                 // Serializer serializer =
327                 // SerializerFactory.getSerializerFactory("xml").makeSerializer(stream,
328                 // format);
329                 // serializer.asDOMSerializer().serialize(document);
330                 // } catch (UnsupportedEncodingException e) {
331                 // } catch (IOException e) {
332                 // } //$NON-NLS-1$
333                 // // Serializer serializer =
334                 // SerializerFactory.getSerializer().makeSerializer(stream, format);
335                 // //$NON-NLS-1$
336                 // } catch (ParserConfigurationException e) {
337                 // throwWriteException(e);
338                 // }
339         }
340
341         private static void throwReadException(Throwable t) throws CoreException {
342                 PHPeclipsePlugin.log(t);
343         }
344
345         private static void throwWriteException(Throwable t) throws CoreException {
346                 PHPeclipsePlugin.log(t);
347         }
348
349 }