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;
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;
43 * <code>PHPSyntaxRdr</code> reads PHP specifics from an XML file (eg.
47 public class PHPSyntaxRdr {
48 // private static final String PHPDEFAULT_FILE = "default-syntax.xml";
50 private static final String PHPSYNTAX_FILE = "syntax.xml"; //$NON-NLS-1$
51 // private static final String USERSYNTAX_FILE = "usersyntax.xml";
53 // private static final String USERDEFAULT_FILE = "default-usersyntax.xml";
56 private static final String PHPSYNTAX_TAG = "s"; //$NON-NLS-1$
58 private static final String KEYWORD_ATTR = "k"; //$NON-NLS-1$
60 private static final String TYPE_ATTR = "t"; //$NON-NLS-1$
62 private static final String CONSTANT_ATTR = "c"; //$NON-NLS-1$
64 private static final String FN_ATTR = "f"; //$NON-NLS-1$
66 private static final String USAGE_ATTR = "u"; //$NON-NLS-1$
67 // private static final String TOKENVAL_ATTR = "tokenval"; //$NON-NLS-1$
69 private static IPreferenceStore store;
71 private static boolean hasXMLFileBeenRead = true;
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;
79 private static ArrayList syntaxdata;
81 public PHPSyntaxRdr() {
82 // see getSyntaxData()
84 store = PHPeclipsePlugin.getDefault().getPreferenceStore();
87 public static void readInSyntax() {
89 hasXMLFileBeenRead = true;
91 * Attempt to read the syntax file from the metadata if this does
92 * not work, create metadata from default
94 File syntaxFile = getSyntaxFile();
95 if (syntaxFile.exists()) {
96 readFromFile(syntaxFile);
98 readFromStream(PHPSyntaxRdr.class
99 .getResourceAsStream(PHPSYNTAX_FILE));
100 saveToFile(syntaxFile);
102 /* Read the user-defined syntax file if it exists */
103 // String buffer = new
104 // String(store.getString(PHPeclipsePlugin.PHP_USERDEF_XMLFILE));
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);
112 } catch (CoreException ce) {
113 ce.printStackTrace();
117 public static void readFromFile(String filename) {
119 readFromFile(new File(filename));
120 } catch (CoreException e) {
124 public static void readFromFile(File file) throws CoreException {
125 InputStream stream = null;
129 stream = new FileInputStream(file);
130 readFromStream(stream);
131 } catch (IOException e) {
132 throwReadException(e);
135 if (stream != null) {
138 } catch (IOException e) {
144 public static void readFromStream(InputStream stream) throws CoreException {
146 DocumentBuilderFactory factory = DocumentBuilderFactory
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);
153 int count = elements.getLength();
154 for (int i = 0; i != count; i++) {
155 Node node = elements.item(i);
156 NamedNodeMap attributes = node.getAttributes();
158 if (attributes == null)
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,
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();
174 buffer.append(value);
176 String description = buffer.toString().trim();
178 if (Keyword == null && Type == null && Function == null
179 && Constant == null) {
180 // ignore as it is not a valid phpsyntax tag
182 if (Keyword != null) {
183 // syntaxdata.add(new PHPKeyword(Keyword, usage,
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,
191 } else if (Constant != null) {
192 syntaxdata.add(new PHPConstant(Constant, null,
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);
210 public static ArrayList getSyntaxData() {
211 if (syntaxdata == null) {
212 syntaxdata = new ArrayList();
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);
227 public static ArrayList getUserSyntaxData() {
228 return userdefsyntaxdata;
231 private static File getSyntaxFile() {
232 IPath path = PHPeclipsePlugin.getDefault().getStateLocation();
233 path = path.append(PHPSYNTAX_FILE);
234 return path.toFile();
237 private static String getAttributeValue(NamedNodeMap attributes, String name) {
238 Node node = attributes.getNamedItem(name);
239 return node == null ? null : node.getNodeValue();
242 public static void saveToFile(File file) throws CoreException {
243 OutputStream stream = null;
245 stream = new FileOutputStream(file);
246 saveToStream(stream);
247 } catch (IOException e) {
248 throwWriteException(e);
253 } catch (IOException e) {
258 public static void saveToStream(OutputStream stream) throws CoreException {
260 DocumentBuilderFactory factory = DocumentBuilderFactory
262 DocumentBuilder builder = factory.newDocumentBuilder();
263 Document document = builder.newDocument();
264 Node root = document.createElement("PHPStandardSyntax"); // $NON-NLS-1$
266 document.appendChild(root);
267 for (int i = 0; i != syntaxdata.size(); i++) {
268 Object bufferobj = (Object) syntaxdata.get(i);
270 Node node = document.createElement(PHPSYNTAX_TAG); // $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);
293 if (bufferobj instanceof PHPFunction) {
294 // Attr usage = document.createAttribute(USAGE_ATTR);
295 Text usage = document
296 .createTextNode(((PHPFunction) bufferobj)
298 node.appendChild(usage);
300 if (bufferobj instanceof PHPConstant) {
301 // Attr usage = document.createAttribute(USAGE_ATTR);
302 Text usage = document
303 .createTextNode(((PHPConstant) bufferobj)
305 node.appendChild(usage);
308 Transformer transformer = TransformerFactory.newInstance()
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);
315 transformer.transform(source, result);
317 } catch (ParserConfigurationException e) {
318 throwWriteException(e);
319 } catch (TransformerException e) {
320 throwWriteException(e);
322 // OutputFormat format = new OutputFormat();
323 // format.setPreserveSpace(true);
325 // Serializer serializer =
326 // SerializerFactory.getSerializerFactory("xml").makeSerializer(stream,
328 // serializer.asDOMSerializer().serialize(document);
329 // } catch (UnsupportedEncodingException e) {
330 // } catch (IOException e) {
332 // // Serializer serializer =
333 // SerializerFactory.getSerializer().makeSerializer(stream, format);
335 // } catch (ParserConfigurationException e) {
336 // throwWriteException(e);
340 private static void throwReadException(Throwable t) throws CoreException {
341 PHPeclipsePlugin.log(t);
344 private static void throwWriteException(Throwable t) throws CoreException {
345 PHPeclipsePlugin.log(t);