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