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. keywords)
46 public class PHPSyntaxRdr {
47 // private static final String PHPDEFAULT_FILE = "default-syntax.xml"; //$NON-NLS-1$
48 private static final String PHPSYNTAX_FILE = "syntax.xml"; //$NON-NLS-1$
49 // private static final String USERSYNTAX_FILE = "usersyntax.xml"; //$NON-NLS-1$
50 // private static final String USERDEFAULT_FILE = "default-usersyntax.xml"; //$NON-NLS-1$
51 private static final String PHPSYNTAX_TAG = "s"; //$NON-NLS-1$
52 private static final String KEYWORD_ATTR = "k"; //$NON-NLS-1$
53 private static final String TYPE_ATTR = "t"; //$NON-NLS-1$
54 private static final String CONSTANT_ATTR = "c"; //$NON-NLS-1$
55 private static final String FN_ATTR = "f"; //$NON-NLS-1$
56 private static final String USAGE_ATTR = "u"; //$NON-NLS-1$
57 // private static final String TOKENVAL_ATTR = "tokenval"; //$NON-NLS-1$
58 private static IPreferenceStore store;
59 private static boolean hasXMLFileBeenRead = true;
61 //The following variable is used to hold the syntax from
62 //the suers custom file - if that file should be changed,
63 //then all entries in this variable should be removed from
64 //the word list, reread from the file and then reinserted.
65 private static ArrayList userdefsyntaxdata;
67 private static ArrayList syntaxdata;
69 public PHPSyntaxRdr() {
70 // see getSyntaxData()
72 store = PHPeclipsePlugin.getDefault().getPreferenceStore();
75 public static void readInSyntax() {
77 hasXMLFileBeenRead = true;
78 /*Attempt to read the syntax file from the metadata
79 * if this does not work, create metadata from default*/
80 File syntaxFile = getSyntaxFile();
81 if (syntaxFile.exists()) {
82 readFromFile(syntaxFile);
84 readFromStream(PHPSyntaxRdr.class.getResourceAsStream(PHPSYNTAX_FILE));
85 saveToFile(syntaxFile);
87 /*Read the user-defined syntax file if it exists*/
88 //String buffer = new String(store.getString(PHPeclipsePlugin.PHP_USERDEF_XMLFILE));
90 store = PHPeclipsePlugin.getDefault().getPreferenceStore();
91 String buffer = new String(store.getString(IPreferenceConstants.PHP_USERDEF_XMLFILE));
92 if (!(buffer.equals("") || buffer == null)) {
95 } catch (CoreException ce) {
100 public static void readFromFile(String filename) {
102 readFromFile(new File(filename));
103 } catch (CoreException e) {
107 public static void readFromFile(File file) throws CoreException {
108 InputStream stream = null;
112 stream = new FileInputStream(file);
113 readFromStream(stream);
114 } catch (IOException e) {
115 throwReadException(e);
118 if (stream != null) {
121 } catch (IOException e) {
126 public static void readFromStream(InputStream stream) throws CoreException {
128 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
129 DocumentBuilder parser = factory.newDocumentBuilder();
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 syntaxdata.add(new PHPKeyword(Keyword, usage));
164 } else if (Type != null) {
165 syntaxdata.add(new PHPType(Type, usage));
166 } else if (Function != null) {
167 syntaxdata.add(new PHPFunction(Function, usage, description));
168 } else if (Constant != null) {
169 syntaxdata.add(new PHPConstant(Constant, null, description));
173 } catch (ParserConfigurationException e) {
174 throwReadException(e);
175 } catch (IOException e) {
176 throwReadException(e);
177 } catch (SAXParseException e) {
178 System.out.println("SAXParseException in line:"+e.getLineNumber()+" column:"+e.getColumnNumber());
179 throwReadException(e);
180 } catch (SAXException e) {
181 throwReadException(e);
185 public static ArrayList getSyntaxData() {
186 if (syntaxdata == null) {
187 syntaxdata = new ArrayList();
193 public static void replaceUserDefFile() {
194 /*Replace the user-defined syntax file if it exists*/
195 String buffer = new String(store.getString(IPreferenceConstants.PHP_USERDEF_XMLFILE));
196 if (!buffer.equals("") || buffer == null) {
197 readFromFile(buffer);
201 public static ArrayList getUserSyntaxData() {
202 return userdefsyntaxdata;
205 private static File getSyntaxFile() {
206 IPath path = PHPeclipsePlugin.getDefault().getStateLocation();
207 path = path.append(PHPSYNTAX_FILE);
208 return path.toFile();
211 private static String getAttributeValue(NamedNodeMap attributes, String name) {
212 Node node = attributes.getNamedItem(name);
213 return node == null ? null : node.getNodeValue();
216 public static void saveToFile(File file) throws CoreException {
217 OutputStream stream = null;
219 stream = new FileOutputStream(file);
220 saveToStream(stream);
221 } catch (IOException e) {
222 throwWriteException(e);
227 } catch (IOException e) {
232 public static void saveToStream(OutputStream stream) throws CoreException {
234 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
235 DocumentBuilder builder = factory.newDocumentBuilder();
236 Document document = builder.newDocument();
237 Node root = document.createElement("PHPStandardSyntax"); // $NON-NLS-1$ //$NON-NLS-1$
238 document.appendChild(root);
239 for (int i = 0; i != syntaxdata.size(); i++) {
240 Object bufferobj = (Object) syntaxdata.get(i);
242 Node node = document.createElement(PHPSYNTAX_TAG); // $NON-NLS-1$ //$NON-NLS-1$
243 root.appendChild(node);
244 NamedNodeMap attributes = node.getAttributes();
245 if (bufferobj instanceof PHPType)
246 name = document.createAttribute(TYPE_ATTR);
247 if (bufferobj instanceof PHPKeyword)
248 name = document.createAttribute(KEYWORD_ATTR);
249 if (bufferobj instanceof PHPFunction)
250 name = document.createAttribute(FN_ATTR);
251 if (bufferobj instanceof PHPConstant)
252 name = document.createAttribute(CONSTANT_ATTR);
253 name.setValue(((PHPElement) bufferobj).getName());
254 attributes.setNamedItem(name);
255 Attr description = document.createAttribute(USAGE_ATTR);
256 description.setValue(((PHPElement) bufferobj).getUsage());
257 attributes.setNamedItem(description);
258 // if (bufferobj instanceof PHPKeyword) {
259 // Attr tokenval = document.createAttribute(TOKENVAL_ATTR);
260 // tokenval.setValue((new Integer(((PHPKeyword) bufferobj).gettokenval())).toString());
261 // attributes.setNamedItem(tokenval);
263 if (bufferobj instanceof PHPFunction) {
264 // Attr usage = document.createAttribute(USAGE_ATTR);
265 Text usage = document.createTextNode(((PHPFunction) bufferobj).getDescription());
266 node.appendChild(usage);
268 if (bufferobj instanceof PHPConstant) {
269 // Attr usage = document.createAttribute(USAGE_ATTR);
270 Text usage = document.createTextNode(((PHPConstant) bufferobj).getDescription());
271 node.appendChild(usage);
274 Transformer transformer=TransformerFactory.newInstance().newTransformer();
275 transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
276 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //$NON-NLS-1$
277 DOMSource source = new DOMSource(document);
278 StreamResult result = new StreamResult(stream);
280 transformer.transform(source, result);
282 } catch (ParserConfigurationException e) {
283 throwWriteException(e);
284 } catch (TransformerException e) {
285 throwWriteException(e);
287 // OutputFormat format = new OutputFormat();
288 // format.setPreserveSpace(true);
290 // Serializer serializer = SerializerFactory.getSerializerFactory("xml").makeSerializer(stream, format);
291 // serializer.asDOMSerializer().serialize(document);
292 // } catch (UnsupportedEncodingException e) {
293 // } catch (IOException e) {
295 // // Serializer serializer = SerializerFactory.getSerializer().makeSerializer(stream, format); //$NON-NLS-1$
296 // } catch (ParserConfigurationException e) {
297 // throwWriteException(e);
301 private static void throwReadException(Throwable t) throws CoreException {
302 PHPeclipsePlugin.log(t);
305 private static void throwWriteException(Throwable t) throws CoreException {
306 PHPeclipsePlugin.log(t);