1 package net.sourceforge.phpeclipse.phpeditor;
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.ArrayList;
12 import javax.xml.parsers.DocumentBuilder;
13 import javax.xml.parsers.DocumentBuilderFactory;
14 import javax.xml.parsers.ParserConfigurationException;
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;
24 import org.apache.xml.serialize.OutputFormat;
25 import org.apache.xml.serialize.Serializer;
26 import org.apache.xml.serialize.SerializerFactory;
27 import org.eclipse.core.runtime.CoreException;
28 import org.eclipse.core.runtime.IPath;
29 import org.eclipse.core.runtime.IStatus;
30 import org.eclipse.jface.preference.IPreferenceStore;
31 import org.w3c.dom.Attr;
32 import org.w3c.dom.Document;
33 import org.w3c.dom.NamedNodeMap;
34 import org.w3c.dom.Node;
35 import org.w3c.dom.NodeList;
36 import org.w3c.dom.Text;
37 import org.xml.sax.InputSource;
38 import org.xml.sax.SAXException;
39 import org.xml.sax.SAXParseException;
42 * <code>PHPSyntaxRdr</code> reads PHP specifics from an XML file (eg. keywords)
45 public class PHPSyntaxRdr {
46 private static final String PHPDEFAULT_FILE = "default-phpsyntax.xml"; //$NON-NLS-1$
47 private static final String PHPSYNTAX_FILE = "phpsyntax.xml"; //$NON-NLS-1$
48 private static final String USERSYNTAX_FILE = "usersyntax.xml"; //$NON-NLS-1$
49 private static final String USERDEFAULT_FILE = "default-usersyntax.xml"; //$NON-NLS-1$
50 private static final String PHPSYNTAX_TAG = "phpsyntax"; //$NON-NLS-1$
51 private static final String KEYWORD_ATTR = "keyword"; //$NON-NLS-1$
52 private static final String TYPE_ATTR = "type"; //$NON-NLS-1$
53 private static final String CONSTANT_ATTR = "constant"; //$NON-NLS-1$
54 private static final String FN_ATTR = "function"; //$NON-NLS-1$
55 private static final String USAGE_ATTR = "usage"; //$NON-NLS-1$
56 private static final String TOKENVAL_ATTR = "tokenval"; //$NON-NLS-1$
57 private static IPreferenceStore store;
58 private static boolean hasXMLFileBeenRead = true;
60 //The following variable is used to hold the syntax from
61 //the suers custom file - if that file should be changed,
62 //then all entries in this variable should be removed from
63 //the word list, reread from the file and then reinserted.
64 private static ArrayList userdefsyntaxdata;
66 private static ArrayList syntaxdata;
68 public PHPSyntaxRdr() {
69 // see getSyntaxData()
71 store = PHPeclipsePlugin.getDefault().getPreferenceStore();
74 public static void readInSyntax() {
76 hasXMLFileBeenRead = true;
77 /*Attempt to read the syntax file from the metadata
78 * if this does not work, create metadata from default*/
79 File syntaxFile = getSyntaxFile();
80 if (syntaxFile.exists()) {
81 readFromFile(syntaxFile);
83 readFromStream(PHPSyntaxRdr.class.getResourceAsStream(PHPSYNTAX_FILE));
84 saveToFile(syntaxFile);
86 /*Read the user-defined syntax file if it exists*/
87 //String buffer = new String(store.getString(PHPeclipsePlugin.PHP_USERDEF_XMLFILE));
89 store = PHPeclipsePlugin.getDefault().getPreferenceStore();
90 String buffer = new String(store.getString(IPreferenceConstants.PHP_USERDEF_XMLFILE));
91 if (!(buffer.equals("") || buffer == null)) {
94 } catch (CoreException ce) {
99 public static void readFromFile(String filename) {
101 readFromFile(new File(filename));
102 } catch (CoreException e) {
106 public static void readFromFile(File file) throws CoreException {
107 InputStream stream = null;
111 stream = new FileInputStream(file);
112 readFromStream(stream);
113 } catch (IOException e) {
114 throwReadException(e);
117 if (stream != null) {
120 } catch (IOException e) {
125 public static void readFromStream(InputStream stream) throws CoreException {
127 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
128 DocumentBuilder parser = factory.newDocumentBuilder();
129 org.apache.crimson.parser.Parser2 pp;
130 Document document = parser.parse(new InputSource(stream));
131 // Read in the Standard PHPSyntax "stuff"
132 NodeList elements = document.getElementsByTagName(PHPSYNTAX_TAG);
134 int count = elements.getLength();
135 for (int i = 0; i != count; i++) {
136 Node node = elements.item(i);
137 NamedNodeMap attributes = node.getAttributes();
139 if (attributes == null)
142 String Keyword = getAttributeValue(attributes, KEYWORD_ATTR);
143 String Type = getAttributeValue(attributes, TYPE_ATTR);
144 String Function = getAttributeValue(attributes, FN_ATTR);
145 String Constant = getAttributeValue(attributes, CONSTANT_ATTR);
146 String usage = getAttributeValue(attributes, USAGE_ATTR);
147 String Tokenval = getAttributeValue(attributes, TOKENVAL_ATTR);
149 StringBuffer buffer = new StringBuffer();
150 NodeList children = node.getChildNodes();
151 for (int j = 0; j != children.getLength(); j++) {
152 String value = children.item(j).getNodeValue();
154 buffer.append(value);
156 String description = buffer.toString().trim();
158 if (Keyword == null && Type == null && Function == null && Constant == null) {
159 //ignore as it is not a valid phpsyntax tag
161 if (Keyword != null) {
162 syntaxdata.add(new PHPKeyword(Keyword, usage, Tokenval));
163 } else if (Type != null) {
164 syntaxdata.add(new PHPType(Type, usage));
165 } else if (Function != null) {
166 syntaxdata.add(new PHPFunction(Function, usage, description));
167 } else if (Constant != null) {
168 syntaxdata.add(new PHPConstant(Constant, usage));
172 } catch (ParserConfigurationException e) {
173 throwReadException(e);
174 } catch (IOException e) {
175 throwReadException(e);
176 } catch (SAXParseException e) {
177 System.out.println("SAXParseException in line:"+e.getLineNumber()+" column:"+e.getColumnNumber());
178 throwReadException(e);
179 } catch (SAXException e) {
180 throwReadException(e);
184 public static ArrayList getSyntaxData() {
185 if (syntaxdata == null) {
186 syntaxdata = new ArrayList();
192 public static void replaceUserDefFile() {
193 /*Replace the user-defined syntax file if it exists*/
194 String buffer = new String(store.getString(IPreferenceConstants.PHP_USERDEF_XMLFILE));
195 if (!buffer.equals("") || buffer == null) {
196 readFromFile(buffer);
200 public static ArrayList getUserSyntaxData() {
201 return userdefsyntaxdata;
204 private static File getSyntaxFile() {
205 IPath path = PHPeclipsePlugin.getDefault().getStateLocation();
206 path = path.append(PHPSYNTAX_FILE);
207 return path.toFile();
210 private static String getAttributeValue(NamedNodeMap attributes, String name) {
211 Node node = attributes.getNamedItem(name);
212 return node == null ? null : node.getNodeValue();
215 public static void saveToFile(File file) throws CoreException {
216 OutputStream stream = null;
218 stream = new FileOutputStream(file);
219 saveToStream(stream);
220 } catch (IOException e) {
221 throwWriteException(e);
226 } catch (IOException e) {
231 public static void saveToStream(OutputStream stream) throws CoreException {
233 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
234 DocumentBuilder builder = factory.newDocumentBuilder();
235 Document document = builder.newDocument();
236 Node root = document.createElement("PHPStandardSyntax"); // $NON-NLS-1$ //$NON-NLS-1$
237 document.appendChild(root);
238 for (int i = 0; i != syntaxdata.size(); i++) {
239 Object bufferobj = (Object) syntaxdata.get(i);
241 Node node = document.createElement(PHPSYNTAX_TAG); // $NON-NLS-1$ //$NON-NLS-1$
242 root.appendChild(node);
243 NamedNodeMap attributes = node.getAttributes();
244 if (bufferobj instanceof PHPType)
245 name = document.createAttribute(TYPE_ATTR);
246 if (bufferobj instanceof PHPKeyword)
247 name = document.createAttribute(KEYWORD_ATTR);
248 if (bufferobj instanceof PHPFunction)
249 name = document.createAttribute(FN_ATTR);
250 if (bufferobj instanceof PHPConstant)
251 name = document.createAttribute(CONSTANT_ATTR);
252 name.setValue(((PHPElement) bufferobj).getName());
253 attributes.setNamedItem(name);
254 Attr description = document.createAttribute(USAGE_ATTR);
255 description.setValue(((PHPElement) bufferobj).getUsage());
256 attributes.setNamedItem(description);
257 if (bufferobj instanceof PHPKeyword) {
258 Attr tokenval = document.createAttribute(TOKENVAL_ATTR);
259 tokenval.setValue((new Integer(((PHPKeyword) bufferobj).gettokenval())).toString());
260 attributes.setNamedItem(tokenval);
262 if (bufferobj instanceof PHPFunction) {
263 // Attr usage = document.createAttribute(USAGE_ATTR);
264 Text usage = document.createTextNode(((PHPFunction) bufferobj).getDescription());
265 node.appendChild(usage);
266 // usage.setValue(((PHPFunction) bufferobj).getUsage());
267 // attributes.setNamedItem(usage);
270 OutputFormat format = new OutputFormat();
271 format.setPreserveSpace(true);
273 Serializer serializer = SerializerFactory.getSerializerFactory("xml").makeSerializer(stream, format);
274 serializer.asDOMSerializer().serialize(document);
275 } catch (UnsupportedEncodingException e) {
276 } catch (IOException e) {
278 // Serializer serializer = SerializerFactory.getSerializer().makeSerializer(stream, format); //$NON-NLS-1$
279 } catch (ParserConfigurationException e) {
280 throwWriteException(e);
281 // } catch (IOException e) {
282 // throwWriteException(e);
286 private static void throwReadException(Throwable t) throws CoreException {
287 PHPeclipsePlugin.log(t);
290 private static void throwWriteException(Throwable t) throws CoreException {
291 PHPeclipsePlugin.log(t);