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-phpsyntax.xml"; //$NON-NLS-1$
48 private static final String PHPSYNTAX_FILE = "phpsyntax.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 = "phpsyntax"; //$NON-NLS-1$
52 private static final String KEYWORD_ATTR = "keyword"; //$NON-NLS-1$
53 private static final String TYPE_ATTR = "type"; //$NON-NLS-1$
54 private static final String CONSTANT_ATTR = "constant"; //$NON-NLS-1$
55 private static final String FN_ATTR = "function"; //$NON-NLS-1$
56 private static final String USAGE_ATTR = "usage"; //$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 } 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, null, description));
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);
267 if (bufferobj instanceof PHPConstant) {
268 // Attr usage = document.createAttribute(USAGE_ATTR);
269 Text usage = document.createTextNode(((PHPConstant) bufferobj).getDescription());
270 node.appendChild(usage);
273 Transformer transformer=TransformerFactory.newInstance().newTransformer();
274 transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
275 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //$NON-NLS-1$
276 DOMSource source = new DOMSource(document);
277 StreamResult result = new StreamResult(stream);
279 transformer.transform(source, result);
281 } catch (ParserConfigurationException e) {
282 throwWriteException(e);
283 } catch (TransformerException e) {
284 throwWriteException(e);
286 // OutputFormat format = new OutputFormat();
287 // format.setPreserveSpace(true);
289 // Serializer serializer = SerializerFactory.getSerializerFactory("xml").makeSerializer(stream, format);
290 // serializer.asDOMSerializer().serialize(document);
291 // } catch (UnsupportedEncodingException e) {
292 // } catch (IOException e) {
294 // // Serializer serializer = SerializerFactory.getSerializer().makeSerializer(stream, format); //$NON-NLS-1$
295 // } catch (ParserConfigurationException e) {
296 // throwWriteException(e);
300 private static void throwReadException(Throwable t) throws CoreException {
301 PHPeclipsePlugin.log(t);
304 private static void throwWriteException(Throwable t) throws CoreException {
305 PHPeclipsePlugin.log(t);