changed the "description" attribute in phpsyntax.xml into multi-line text between...
[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.io.UnsupportedEncodingException;
10 import java.util.Vector;
11
12 import javax.xml.parsers.DocumentBuilder;
13 import javax.xml.parsers.DocumentBuilderFactory;
14 import javax.xml.parsers.ParserConfigurationException;
15
16 import net.sourceforge.phpeclipse.IPreferenceConstants;
17 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
18 import net.sourceforge.phpeclipse.phpeditor.php.PHPConstant;
19 import net.sourceforge.phpeclipse.phpeditor.php.PHPElement;
20 import net.sourceforge.phpeclipse.phpeditor.php.PHPFunction;
21 import net.sourceforge.phpeclipse.phpeditor.php.PHPKeyword;
22 import net.sourceforge.phpeclipse.phpeditor.php.PHPType;
23 import org.apache.xml.serialize.OutputFormat;
24 import org.apache.xml.serialize.Serializer;
25 import org.apache.xml.serialize.SerializerFactory;
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.core.runtime.IPath;
28 import org.eclipse.jface.preference.IPreferenceStore;
29 import org.w3c.dom.Attr;
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;
37
38 /**
39  * <code>PHPSyntaxRdr</code> reads PHP specifics from an XML file (eg. keywords) 
40  */
41
42 public class PHPSyntaxRdr {
43   private static final String PHPDEFAULT_FILE = "default-phpsyntax.xml"; //$NON-NLS-1$
44   private static final String PHPSYNTAX_FILE = "phpsyntax.xml"; //$NON-NLS-1$
45   private static final String USERSYNTAX_FILE = "usersyntax.xml"; //$NON-NLS-1$
46   private static final String USERDEFAULT_FILE = "default-usersyntax.xml"; //$NON-NLS-1$
47   private static final String PHPSYNTAX_TAG = "phpsyntax"; //$NON-NLS-1$
48   private static final String KEYWORD_ATTR = "keyword"; //$NON-NLS-1$
49   private static final String TYPE_ATTR = "type"; //$NON-NLS-1$
50   private static final String CONSTANT_ATTR = "constant"; //$NON-NLS-1$
51   private static final String FN_ATTR = "function"; //$NON-NLS-1$
52   private static final String USAGE_ATTR = "usage"; //$NON-NLS-1$
53   private static final String TOKENVAL_ATTR = "tokenval"; //$NON-NLS-1$
54   private static IPreferenceStore store;
55   private static boolean hasXMLFileBeenRead = true;
56
57   //The following variable is used to hold the syntax from
58   //the suers custom file - if that file should be changed,
59   //then all entries in this variable should be removed from
60   //the word list, reread from the file and then reinserted.
61   private static Vector userdefsyntaxdata;
62
63   private static Vector syntaxdata;
64
65   public PHPSyntaxRdr() {
66     syntaxdata = new Vector();
67     store = PHPeclipsePlugin.getDefault().getPreferenceStore();
68   }
69
70   public static void readInSyntax() {
71     try {
72       hasXMLFileBeenRead = true;
73       /*Attempt to read the syntax file from the metadata
74        * if this does not work, create metadata from default*/
75       File syntaxFile = getSyntaxFile();
76       if (syntaxFile.exists()) {
77         readFromFile(syntaxFile);
78       } else {
79         readFromStream(PHPSyntaxRdr.class.getResourceAsStream(PHPSYNTAX_FILE));
80         saveToFile(syntaxFile);
81       }
82       /*Read the user-defined syntax file if it exists*/
83       //String buffer = new String(store.getString(PHPeclipsePlugin.PHP_USERDEF_XMLFILE));
84       if (store == null)
85         store = PHPeclipsePlugin.getDefault().getPreferenceStore();
86       String buffer = new String(store.getString(IPreferenceConstants.PHP_USERDEF_XMLFILE));
87       if (!buffer.equals("") || buffer != null) {
88         readFromFile(buffer);
89       }
90     } catch (CoreException ce) {
91       ce.printStackTrace();
92     }
93   }
94
95   public static void readFromFile(String filename) {
96     try {
97       readFromFile(new File(filename));
98     } catch (CoreException e) {
99     }
100   }
101
102   public static void readFromFile(File file) throws CoreException {
103     InputStream stream = null;
104     try {
105       stream = new FileInputStream(file);
106       readFromStream(stream);
107     } catch (IOException e) {
108       throwReadException(e);
109     } finally {
110       try {
111         if (stream != null) {
112           stream.close();
113         }
114       } catch (IOException e) {
115       }
116     }
117   }
118   public static void readFromStream(InputStream stream) throws CoreException {
119     try {
120       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
121       DocumentBuilder parser = factory.newDocumentBuilder();
122       Document document = parser.parse(new InputSource(stream));
123       //Read in the Standard PHPSyntax "stuff"
124       NodeList elements = document.getElementsByTagName(PHPSYNTAX_TAG);
125
126       int count = elements.getLength();
127       for (int i = 0; i != count; i++) {
128         Node node = elements.item(i);
129         NamedNodeMap attributes = node.getAttributes();
130
131         if (attributes == null)
132           continue;
133
134         String Keyword = getAttributeValue(attributes, KEYWORD_ATTR);
135         String Type = getAttributeValue(attributes, TYPE_ATTR);
136         String Function = getAttributeValue(attributes, FN_ATTR);
137         String Constant = getAttributeValue(attributes, CONSTANT_ATTR);
138         String usage = getAttributeValue(attributes, USAGE_ATTR);
139         String Tokenval = getAttributeValue(attributes, TOKENVAL_ATTR);
140
141         StringBuffer buffer= new StringBuffer();
142         NodeList children= node.getChildNodes();
143         for (int j= 0; j != children.getLength(); j++) {
144           String value= children.item(j).getNodeValue();
145           if (value != null)
146             buffer.append(value);
147         }
148         String description = buffer.toString().trim();
149         
150         if (Keyword == null && Type == null && Function == null && Constant == null) {
151           //ignore as it is not a valid phpsyntax tag
152         } else {
153           if (Keyword != null) {
154             syntaxdata.addElement(new PHPKeyword(Keyword, usage, Tokenval));
155           } else if (Type != null) {
156             syntaxdata.addElement(new PHPType(Type, usage));
157           } else if (Function != null) {
158             syntaxdata.addElement(new PHPFunction(Function, usage, description));
159           } else if (Constant != null) {
160             syntaxdata.addElement(new PHPConstant(Constant, usage));
161           }
162         }
163       }
164     } catch (ParserConfigurationException e) {
165       throwReadException(e);
166     } catch (IOException e) {
167       throwReadException(e);
168     } catch (SAXException e) {
169       throwReadException(e);
170     }
171   }
172
173   public static Vector getsyntaxdata() {
174     return (Vector) syntaxdata.clone();
175   }
176
177   public static void replaceUserDefFile() {
178     /*Replace the user-defined syntax file if it exists*/
179     String buffer = new String(store.getString(IPreferenceConstants.PHP_USERDEF_XMLFILE));
180     if (!buffer.equals("") || buffer == null) {
181       readFromFile(buffer);
182     }
183   }
184
185   public static Vector getUserDefinitions() {
186     return (Vector) userdefsyntaxdata.clone();
187   }
188
189   private static File getSyntaxFile() {
190     IPath path = PHPeclipsePlugin.getDefault().getStateLocation();
191     path = path.append(PHPSYNTAX_FILE);
192     return path.toFile();
193   }
194
195   private static String getAttributeValue(NamedNodeMap attributes, String name) {
196     Node node = attributes.getNamedItem(name);
197     return node == null ? null : node.getNodeValue();
198   }
199
200   public static void saveToFile(File file) throws CoreException {
201     OutputStream stream = null;
202     try {
203       stream = new FileOutputStream(file);
204       saveToStream(stream);
205     } catch (IOException e) {
206       throwWriteException(e);
207     } finally {
208       try {
209         if (stream != null)
210           stream.close();
211       } catch (IOException e) {
212       }
213     }
214   }
215
216   public static void saveToStream(OutputStream stream) throws CoreException {
217     try {
218       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
219       DocumentBuilder builder = factory.newDocumentBuilder();
220       Document document = builder.newDocument();
221       Node root = document.createElement("PHPStandardSyntax"); // $NON-NLS-1$ //$NON-NLS-1$
222       document.appendChild(root);
223       for (int i = 0; i != syntaxdata.size(); i++) {
224         Object bufferobj = (Object) syntaxdata.get(i);
225         Attr name = null;
226         Node node = document.createElement(PHPSYNTAX_TAG); // $NON-NLS-1$ //$NON-NLS-1$
227         root.appendChild(node);
228         NamedNodeMap attributes = node.getAttributes();
229         if (bufferobj instanceof PHPType)
230           name = document.createAttribute(TYPE_ATTR);
231         if (bufferobj instanceof PHPKeyword)
232           name = document.createAttribute(KEYWORD_ATTR);
233         if (bufferobj instanceof PHPFunction)
234           name = document.createAttribute(FN_ATTR);
235         if (bufferobj instanceof PHPConstant)
236           name = document.createAttribute(CONSTANT_ATTR);
237         name.setValue(((PHPElement) bufferobj).getName());
238         attributes.setNamedItem(name);
239         Attr description = document.createAttribute(USAGE_ATTR);
240         description.setValue(((PHPElement) bufferobj).getUsage());
241         attributes.setNamedItem(description);
242         if (bufferobj instanceof PHPKeyword) {
243           Attr tokenval = document.createAttribute(TOKENVAL_ATTR);
244           tokenval.setValue((new Integer(((PHPKeyword) bufferobj).gettokenval())).toString());
245           attributes.setNamedItem(tokenval);
246         }
247         if (bufferobj instanceof PHPFunction) {
248     //      Attr usage = document.createAttribute(USAGE_ATTR);
249           Text usage = document.createTextNode(((PHPFunction) bufferobj).getDescription());
250           node.appendChild(usage);
251 //          usage.setValue(((PHPFunction) bufferobj).getUsage());
252 //          attributes.setNamedItem(usage);
253         }
254       }
255       OutputFormat format = new OutputFormat();
256       format.setPreserveSpace(true);
257       try {
258         Serializer serializer = SerializerFactory.getSerializerFactory("xml").makeSerializer(stream, format);
259         serializer.asDOMSerializer().serialize(document);
260       } catch (UnsupportedEncodingException e) {
261       } catch (IOException e) {
262       } //$NON-NLS-1$
263       //                        Serializer serializer = SerializerFactory.getSerializer().makeSerializer(stream, format); //$NON-NLS-1$
264     } catch (ParserConfigurationException e) {
265       throwWriteException(e);
266       //        } catch (IOException e) {
267       //            throwWriteException(e);
268     }
269   }
270
271   private static void throwReadException(Throwable t) throws CoreException {
272     PHPeclipsePlugin.log(t);
273     //          IStatus status= new JavaUIStatus(JavaStatusConstants.TEMPLATE_IO_EXCEPTION,
274     //                  TemplateMessages.getString("TemplateSet.error.read"), t); //$NON-NLS-1$
275     //          throw new JavaUIException(status);
276   }
277
278   private static void throwWriteException(Throwable t) throws CoreException {
279     PHPeclipsePlugin.log(t);
280     //          IStatus status= new JavaUIStatus(JavaStatusConstants.TEMPLATE_IO_EXCEPTION,
281     //                  TemplateMessages.getString("TemplateSet.error.write"), t); //$NON-NLS-1$
282     //          throw new JavaUIException(status);
283   }
284
285 }