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.util.ArrayList;
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;
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;
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;
44 * <code>PHPSyntaxRdr</code> reads PHP specifics from an XML file (eg.
48 public class PHPSyntaxRdr {
49 // private static final String PHPDEFAULT_FILE = "default-syntax.xml";
51 private static final String PHPSYNTAX_FILE = "syntax.xml"; //$NON-NLS-1$
52 // private static final String USERSYNTAX_FILE = "usersyntax.xml";
54 // private static final String USERDEFAULT_FILE = "default-usersyntax.xml";
57 private static final String PHPSYNTAX_TAG = "s"; //$NON-NLS-1$
59 private static final String KEYWORD_ATTR = "k"; //$NON-NLS-1$
61 private static final String TYPE_ATTR = "t"; //$NON-NLS-1$
63 private static final String CONSTANT_ATTR = "c"; //$NON-NLS-1$
65 private static final String FN_ATTR = "f"; //$NON-NLS-1$
67 private static final String USAGE_ATTR = "u"; //$NON-NLS-1$
68 // private static final String TOKENVAL_ATTR = "tokenval"; //$NON-NLS-1$
70 private static IPreferenceStore store;
72 //private static boolean hasXMLFileBeenRead = true;
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;
80 private static ArrayList syntaxdata;
82 public PHPSyntaxRdr() {
83 // see getSyntaxData()
85 store = WebUI.getDefault().getPreferenceStore();
88 public static void readInSyntax() {
90 // hasXMLFileBeenRead = true;
92 * Attempt to read the syntax file from the metadata if this does
93 * not work, create metadata from default
95 /*File syntaxFile = getSyntaxFile();
96 if (syntaxFile.exists()) {
97 readFromFile(syntaxFile);
99 readFromStream(PHPSyntaxRdr.class
100 .getResourceAsStream(PHPSYNTAX_FILE));
101 //saveToFile(syntaxFile);
103 /* Read the user-defined syntax file if it exists */
104 // String buffer = new
105 // String(store.getString(PHPeclipsePlugin.PHP_USERDEF_XMLFILE));
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);
113 } catch (CoreException ce) {
114 ce.printStackTrace();
118 public static void readFromFile(String filename) {
120 readFromFile(new File(filename));
121 } catch (CoreException e) {
125 public static void readFromFile(File file) throws CoreException {
126 InputStream stream = null;
130 stream = new FileInputStream(file);
131 readFromStream(stream);
132 } catch (IOException e) {
133 throwReadException(e);
136 if (stream != null) {
139 } catch (IOException e) {
145 public static void readFromStream(InputStream stream) throws CoreException {
147 DocumentBuilderFactory factory = DocumentBuilderFactory
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);
154 int count = elements.getLength();
155 for (int i = 0; i != count; i++) {
156 Node node = elements.item(i);
157 NamedNodeMap attributes = node.getAttributes();
159 if (attributes == null)
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,
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();
175 buffer.append(value);
177 String description = buffer.toString().trim();
179 if (Keyword == null && Type == null && Function == null
180 && Constant == null) {
181 // ignore as it is not a valid phpsyntax tag
183 if (Keyword != null) {
184 // syntaxdata.add(new PHPKeyword(Keyword, usage,
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,
192 } else if (Constant != null) {
193 syntaxdata.add(new PHPConstant(Constant, null,
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);
211 public static ArrayList getSyntaxData() {
212 if (syntaxdata == null) {
213 syntaxdata = new ArrayList();
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);
228 public static ArrayList getUserSyntaxData() {
229 return userdefsyntaxdata;
232 // private static File getSyntaxFile() {
233 // IPath path = PHPeclipsePlugin.getDefault().getStateLocation();
234 // path = path.append(PHPSYNTAX_FILE);
235 // return path.toFile();
238 private static String getAttributeValue(NamedNodeMap attributes, String name) {
239 Node node = attributes.getNamedItem(name);
240 return node == null ? null : node.getNodeValue();
243 public static void saveToFile(File file) throws CoreException {
244 OutputStream stream = null;
246 stream = new FileOutputStream(file);
247 saveToStream(stream);
248 } catch (IOException e) {
249 throwWriteException(e);
254 } catch (IOException e) {
259 public static void saveToStream(OutputStream stream) throws CoreException {
261 DocumentBuilderFactory factory = DocumentBuilderFactory
263 DocumentBuilder builder = factory.newDocumentBuilder();
264 Document document = builder.newDocument();
265 Node root = document.createElement("PHPStandardSyntax"); // $NON-NLS-1$
267 document.appendChild(root);
268 for (int i = 0; i != syntaxdata.size(); i++) {
269 Object bufferobj = (Object) syntaxdata.get(i);
271 Node node = document.createElement(PHPSYNTAX_TAG); // $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);
294 if (bufferobj instanceof PHPFunction) {
295 // Attr usage = document.createAttribute(USAGE_ATTR);
296 Text usage = document
297 .createTextNode(((PHPFunction) bufferobj)
299 node.appendChild(usage);
301 if (bufferobj instanceof PHPConstant) {
302 // Attr usage = document.createAttribute(USAGE_ATTR);
303 Text usage = document
304 .createTextNode(((PHPConstant) bufferobj)
306 node.appendChild(usage);
309 Transformer transformer = TransformerFactory.newInstance()
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);
316 transformer.transform(source, result);
318 } catch (ParserConfigurationException e) {
319 throwWriteException(e);
320 } catch (TransformerException e) {
321 throwWriteException(e);
323 // OutputFormat format = new OutputFormat();
324 // format.setPreserveSpace(true);
326 // Serializer serializer =
327 // SerializerFactory.getSerializerFactory("xml").makeSerializer(stream,
329 // serializer.asDOMSerializer().serialize(document);
330 // } catch (UnsupportedEncodingException e) {
331 // } catch (IOException e) {
333 // // Serializer serializer =
334 // SerializerFactory.getSerializer().makeSerializer(stream, format);
336 // } catch (ParserConfigurationException e) {
337 // throwWriteException(e);
341 private static void throwReadException(Throwable t) throws CoreException {
342 PHPeclipsePlugin.log(t);
345 private static void throwWriteException(Throwable t) throws CoreException {
346 PHPeclipsePlugin.log(t);