misc changes
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / PHPSyntaxRdr.java
index 6750da0..ba4366c 100644 (file)
@@ -7,7 +7,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.UnsupportedEncodingException;
-import java.util.Vector;
+import java.util.ArrayList;
 
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
@@ -26,6 +26,7 @@ import org.apache.xml.serialize.Serializer;
 import org.apache.xml.serialize.SerializerFactory;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.IStatus;
 import org.eclipse.jface.preference.IPreferenceStore;
 import org.w3c.dom.Attr;
 import org.w3c.dom.Document;
@@ -35,6 +36,7 @@ import org.w3c.dom.NodeList;
 import org.w3c.dom.Text;
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
 
 /**
  * <code>PHPSyntaxRdr</code> reads PHP specifics from an XML file (eg. keywords) 
@@ -59,12 +61,13 @@ public class PHPSyntaxRdr {
   //the suers custom file - if that file should be changed,
   //then all entries in this variable should be removed from
   //the word list, reread from the file and then reinserted.
-  private static Vector userdefsyntaxdata;
+  private static ArrayList userdefsyntaxdata;
 
-  private static Vector syntaxdata;
+  private static ArrayList syntaxdata;
 
   public PHPSyntaxRdr() {
-    syntaxdata = new Vector();
+    // see getSyntaxData()
+    syntaxdata = null;
     store = PHPeclipsePlugin.getDefault().getPreferenceStore();
   }
 
@@ -85,7 +88,7 @@ public class PHPSyntaxRdr {
       if (store == null)
         store = PHPeclipsePlugin.getDefault().getPreferenceStore();
       String buffer = new String(store.getString(IPreferenceConstants.PHP_USERDEF_XMLFILE));
-      if (!buffer.equals("") || buffer != null) {
+      if (!(buffer.equals("") || buffer == null)) {
         readFromFile(buffer);
       }
     } catch (CoreException ce) {
@@ -102,17 +105,20 @@ public class PHPSyntaxRdr {
 
   public static void readFromFile(File file) throws CoreException {
     InputStream stream = null;
-    try {
-      stream = new FileInputStream(file);
-      readFromStream(stream);
-    } catch (IOException e) {
-      throwReadException(e);
-    } finally {
+
+    if (file.exists()) {
       try {
-        if (stream != null) {
-          stream.close();
-        }
+        stream = new FileInputStream(file);
+        readFromStream(stream);
       } catch (IOException e) {
+        throwReadException(e);
+      } finally {
+        try {
+          if (stream != null) {
+            stream.close();
+          }
+        } catch (IOException e) {
+        }
       }
     }
   }
@@ -120,8 +126,9 @@ public class PHPSyntaxRdr {
     try {
       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
       DocumentBuilder parser = factory.newDocumentBuilder();
+      org.apache.crimson.parser.Parser2 pp;
       Document document = parser.parse(new InputSource(stream));
-      //Read in the Standard PHPSyntax "stuff"
+      //               Read in the Standard PHPSyntax "stuff"
       NodeList elements = document.getElementsByTagName(PHPSYNTAX_TAG);
 
       int count = elements.getLength();
@@ -138,27 +145,27 @@ public class PHPSyntaxRdr {
         String Constant = getAttributeValue(attributes, CONSTANT_ATTR);
         String usage = getAttributeValue(attributes, USAGE_ATTR);
         String Tokenval = getAttributeValue(attributes, TOKENVAL_ATTR);
-
-        StringBuffer buffer= new StringBuffer();
-        NodeList children= node.getChildNodes();
-        for (int j= 0; j != children.getLength(); j++) {
-          String value= children.item(j).getNodeValue();
+      
+        StringBuffer buffer = new StringBuffer();
+        NodeList children = node.getChildNodes();
+        for (int j = 0; j != children.getLength(); j++) {
+          String value = children.item(j).getNodeValue();
           if (value != null)
             buffer.append(value);
         }
         String description = buffer.toString().trim();
-        
+
         if (Keyword == null && Type == null && Function == null && Constant == null) {
           //ignore as it is not a valid phpsyntax tag
         } else {
           if (Keyword != null) {
-            syntaxdata.addElement(new PHPKeyword(Keyword, usage, Tokenval));
+            syntaxdata.add(new PHPKeyword(Keyword, usage, Tokenval));
           } else if (Type != null) {
-            syntaxdata.addElement(new PHPType(Type, usage));
+            syntaxdata.add(new PHPType(Type, usage));
           } else if (Function != null) {
-            syntaxdata.addElement(new PHPFunction(Function, usage, description));
+            syntaxdata.add(new PHPFunction(Function, usage, description));
           } else if (Constant != null) {
-            syntaxdata.addElement(new PHPConstant(Constant, usage));
+            syntaxdata.add(new PHPConstant(Constant, usage));
           }
         }
       }
@@ -166,13 +173,20 @@ public class PHPSyntaxRdr {
       throwReadException(e);
     } catch (IOException e) {
       throwReadException(e);
+    } catch (SAXParseException e) {
+      System.out.println("SAXParseException in line:"+e.getLineNumber()+" column:"+e.getColumnNumber());
+      throwReadException(e);
     } catch (SAXException e) {
       throwReadException(e);
     }
   }
 
-  public static Vector getsyntaxdata() {
-    return (Vector) syntaxdata.clone();
+  public static ArrayList getSyntaxData() {
+    if (syntaxdata == null) {
+      syntaxdata = new ArrayList();
+      readInSyntax();
+    }
+    return syntaxdata;
   }
 
   public static void replaceUserDefFile() {
@@ -183,8 +197,8 @@ public class PHPSyntaxRdr {
     }
   }
 
-  public static Vector getUserDefinitions() {
-    return (Vector) userdefsyntaxdata.clone();
+  public static ArrayList getUserSyntaxData() {
+    return userdefsyntaxdata;
   }
 
   private static File getSyntaxFile() {
@@ -246,11 +260,11 @@ public class PHPSyntaxRdr {
           attributes.setNamedItem(tokenval);
         }
         if (bufferobj instanceof PHPFunction) {
-    //      Attr usage = document.createAttribute(USAGE_ATTR);
+          //      Attr usage = document.createAttribute(USAGE_ATTR);
           Text usage = document.createTextNode(((PHPFunction) bufferobj).getDescription());
           node.appendChild(usage);
-//          usage.setValue(((PHPFunction) bufferobj).getUsage());
-//          attributes.setNamedItem(usage);
+          //          usage.setValue(((PHPFunction) bufferobj).getUsage());
+          //          attributes.setNamedItem(usage);
         }
       }
       OutputFormat format = new OutputFormat();
@@ -271,16 +285,10 @@ public class PHPSyntaxRdr {
 
   private static void throwReadException(Throwable t) throws CoreException {
     PHPeclipsePlugin.log(t);
-    //         IStatus status= new JavaUIStatus(JavaStatusConstants.TEMPLATE_IO_EXCEPTION,
-    //                 TemplateMessages.getString("TemplateSet.error.read"), t); //$NON-NLS-1$
-    //         throw new JavaUIException(status);
   }
 
   private static void throwWriteException(Throwable t) throws CoreException {
     PHPeclipsePlugin.log(t);
-    //         IStatus status= new JavaUIStatus(JavaStatusConstants.TEMPLATE_IO_EXCEPTION,
-    //                 TemplateMessages.getString("TemplateSet.error.write"), t); //$NON-NLS-1$
-    //         throw new JavaUIException(status);
   }
 
 }