fixed accelerator problem; slightly improved PHP Perspective
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / PHPSyntaxRdr.java
index 6750da0..20f6404 100644 (file)
@@ -41,246 +41,272 @@ import org.xml.sax.SAXException;
  */
 
 public class PHPSyntaxRdr {
-  private static final String PHPDEFAULT_FILE = "default-phpsyntax.xml"; //$NON-NLS-1$
-  private static final String PHPSYNTAX_FILE = "phpsyntax.xml"; //$NON-NLS-1$
-  private static final String USERSYNTAX_FILE = "usersyntax.xml"; //$NON-NLS-1$
-  private static final String USERDEFAULT_FILE = "default-usersyntax.xml"; //$NON-NLS-1$
-  private static final String PHPSYNTAX_TAG = "phpsyntax"; //$NON-NLS-1$
-  private static final String KEYWORD_ATTR = "keyword"; //$NON-NLS-1$
-  private static final String TYPE_ATTR = "type"; //$NON-NLS-1$
-  private static final String CONSTANT_ATTR = "constant"; //$NON-NLS-1$
-  private static final String FN_ATTR = "function"; //$NON-NLS-1$
-  private static final String USAGE_ATTR = "usage"; //$NON-NLS-1$
-  private static final String TOKENVAL_ATTR = "tokenval"; //$NON-NLS-1$
-  private static IPreferenceStore store;
-  private static boolean hasXMLFileBeenRead = true;
+       private static final String PHPDEFAULT_FILE = "default-phpsyntax.xml"; //$NON-NLS-1$
+       private static final String PHPSYNTAX_FILE = "phpsyntax.xml"; //$NON-NLS-1$
+       private static final String USERSYNTAX_FILE = "usersyntax.xml"; //$NON-NLS-1$
+       private static final String USERDEFAULT_FILE = "default-usersyntax.xml"; //$NON-NLS-1$
+       private static final String PHPSYNTAX_TAG = "phpsyntax"; //$NON-NLS-1$
+       private static final String KEYWORD_ATTR = "keyword"; //$NON-NLS-1$
+       private static final String TYPE_ATTR = "type"; //$NON-NLS-1$
+       private static final String CONSTANT_ATTR = "constant"; //$NON-NLS-1$
+       private static final String FN_ATTR = "function"; //$NON-NLS-1$
+       private static final String USAGE_ATTR = "usage"; //$NON-NLS-1$
+       private static final String TOKENVAL_ATTR = "tokenval"; //$NON-NLS-1$
+       private static IPreferenceStore store;
+       private static boolean hasXMLFileBeenRead = true;
 
-  //The following variable is used to hold the syntax from
-  //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;
+       //The following variable is used to hold the syntax from
+       //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 Vector syntaxdata;
+       private static Vector syntaxdata;
 
-  public PHPSyntaxRdr() {
-    syntaxdata = new Vector();
-    store = PHPeclipsePlugin.getDefault().getPreferenceStore();
-  }
+       public PHPSyntaxRdr() {
+               syntaxdata = new Vector();
+               store = PHPeclipsePlugin.getDefault().getPreferenceStore();
+       }
 
-  public static void readInSyntax() {
-    try {
-      hasXMLFileBeenRead = true;
-      /*Attempt to read the syntax file from the metadata
-       * if this does not work, create metadata from default*/
-      File syntaxFile = getSyntaxFile();
-      if (syntaxFile.exists()) {
-        readFromFile(syntaxFile);
-      } else {
-        readFromStream(PHPSyntaxRdr.class.getResourceAsStream(PHPSYNTAX_FILE));
-        saveToFile(syntaxFile);
-      }
-      /*Read the user-defined syntax file if it exists*/
-      //String buffer = new String(store.getString(PHPeclipsePlugin.PHP_USERDEF_XMLFILE));
-      if (store == null)
-        store = PHPeclipsePlugin.getDefault().getPreferenceStore();
-      String buffer = new String(store.getString(IPreferenceConstants.PHP_USERDEF_XMLFILE));
-      if (!buffer.equals("") || buffer != null) {
-        readFromFile(buffer);
-      }
-    } catch (CoreException ce) {
-      ce.printStackTrace();
-    }
-  }
+       public static void readInSyntax() {
+               try {
+                       hasXMLFileBeenRead = true;
+                       /*Attempt to read the syntax file from the metadata
+                        * if this does not work, create metadata from default*/
+                       File syntaxFile = getSyntaxFile();
+                       if (syntaxFile.exists()) {
+                               readFromFile(syntaxFile);
+                       } else {
+                               readFromStream(
+                                       PHPSyntaxRdr.class.getResourceAsStream(PHPSYNTAX_FILE));
+                               saveToFile(syntaxFile);
+                       }
+                       /*Read the user-defined syntax file if it exists*/
+                       //String buffer = new String(store.getString(PHPeclipsePlugin.PHP_USERDEF_XMLFILE));
+                       if (store == null)
+                               store = PHPeclipsePlugin.getDefault().getPreferenceStore();
+                       String buffer =
+                               new String(
+                                       store.getString(IPreferenceConstants.PHP_USERDEF_XMLFILE));
+                       if (!(buffer.equals("") || buffer == null)) {
+                               readFromFile(buffer);
+                       }
+               } catch (CoreException ce) {
+                       ce.printStackTrace();
+               }
+       }
 
-  public static void readFromFile(String filename) {
-    try {
-      readFromFile(new File(filename));
-    } catch (CoreException e) {
-    }
-  }
+       public static void readFromFile(String filename) {
+               try {
+                       readFromFile(new File(filename));
+               } catch (CoreException e) {
+               }
+       }
 
-  public static void readFromFile(File file) throws CoreException {
-    InputStream stream = null;
-    try {
-      stream = new FileInputStream(file);
-      readFromStream(stream);
-    } catch (IOException e) {
-      throwReadException(e);
-    } finally {
-      try {
-        if (stream != null) {
-          stream.close();
-        }
-      } catch (IOException e) {
-      }
-    }
-  }
-  public static void readFromStream(InputStream stream) throws CoreException {
-    try {
-      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
-      DocumentBuilder parser = factory.newDocumentBuilder();
-      Document document = parser.parse(new InputSource(stream));
-      //Read in the Standard PHPSyntax "stuff"
-      NodeList elements = document.getElementsByTagName(PHPSYNTAX_TAG);
+       public static void readFromFile(File file) throws CoreException {
+               InputStream stream = null;
 
-      int count = elements.getLength();
-      for (int i = 0; i != count; i++) {
-        Node node = elements.item(i);
-        NamedNodeMap attributes = node.getAttributes();
+               if (file.exists()) {
+                       try {
+                               stream = new FileInputStream(file);
+                               readFromStream(stream);
+                       } catch (IOException e) {
+                               throwReadException(e);
+                       } finally {
+                               try {
+                                       if (stream != null) {
+                                               stream.close();
+                                       }
+                               } catch (IOException e) {
+                               }
+                       }
+               }
+       }
+       public static void readFromStream(InputStream stream)
+               throws CoreException {
+               try {
+                       DocumentBuilderFactory factory =
+                               DocumentBuilderFactory.newInstance();
+                       DocumentBuilder parser = factory.newDocumentBuilder();
+                       Document document = parser.parse(new InputSource(stream));
+                       //Read in the Standard PHPSyntax "stuff"
+                       NodeList elements = document.getElementsByTagName(PHPSYNTAX_TAG);
 
-        if (attributes == null)
-          continue;
+                       int count = elements.getLength();
+                       for (int i = 0; i != count; i++) {
+                               Node node = elements.item(i);
+                               NamedNodeMap attributes = node.getAttributes();
 
-        String Keyword = getAttributeValue(attributes, KEYWORD_ATTR);
-        String Type = getAttributeValue(attributes, TYPE_ATTR);
-        String Function = getAttributeValue(attributes, FN_ATTR);
-        String Constant = getAttributeValue(attributes, CONSTANT_ATTR);
-        String usage = getAttributeValue(attributes, USAGE_ATTR);
-        String Tokenval = getAttributeValue(attributes, TOKENVAL_ATTR);
+                               if (attributes == null)
+                                       continue;
 
-        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));
-          } else if (Type != null) {
-            syntaxdata.addElement(new PHPType(Type, usage));
-          } else if (Function != null) {
-            syntaxdata.addElement(new PHPFunction(Function, usage, description));
-          } else if (Constant != null) {
-            syntaxdata.addElement(new PHPConstant(Constant, usage));
-          }
-        }
-      }
-    } catch (ParserConfigurationException e) {
-      throwReadException(e);
-    } catch (IOException e) {
-      throwReadException(e);
-    } catch (SAXException e) {
-      throwReadException(e);
-    }
-  }
+                               String Keyword = getAttributeValue(attributes, KEYWORD_ATTR);
+                               String Type = getAttributeValue(attributes, TYPE_ATTR);
+                               String Function = getAttributeValue(attributes, FN_ATTR);
+                               String Constant = getAttributeValue(attributes, CONSTANT_ATTR);
+                               String usage = getAttributeValue(attributes, USAGE_ATTR);
+                               String Tokenval = getAttributeValue(attributes, TOKENVAL_ATTR);
 
-  public static Vector getsyntaxdata() {
-    return (Vector) syntaxdata.clone();
-  }
+                               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();
 
-  public static void replaceUserDefFile() {
-    /*Replace the user-defined syntax file if it exists*/
-    String buffer = new String(store.getString(IPreferenceConstants.PHP_USERDEF_XMLFILE));
-    if (!buffer.equals("") || buffer == null) {
-      readFromFile(buffer);
-    }
-  }
+                               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));
+                                       } else if (Type != null) {
+                                               syntaxdata.addElement(new PHPType(Type, usage));
+                                       } else if (Function != null) {
+                                               syntaxdata.addElement(
+                                                       new PHPFunction(Function, usage, description));
+                                       } else if (Constant != null) {
+                                               syntaxdata.addElement(new PHPConstant(Constant, usage));
+                                       }
+                               }
+                       }
+               } catch (ParserConfigurationException e) {
+                       throwReadException(e);
+               } catch (IOException e) {
+                       throwReadException(e);
+               } catch (SAXException e) {
+                       throwReadException(e);
+               }
+       }
 
-  public static Vector getUserDefinitions() {
-    return (Vector) userdefsyntaxdata.clone();
-  }
+       public static Vector getsyntaxdata() {
+               return (Vector) syntaxdata.clone();
+       }
 
-  private static File getSyntaxFile() {
-    IPath path = PHPeclipsePlugin.getDefault().getStateLocation();
-    path = path.append(PHPSYNTAX_FILE);
-    return path.toFile();
-  }
+       public static void replaceUserDefFile() {
+               /*Replace the user-defined syntax file if it exists*/
+               String buffer =
+                       new String(
+                               store.getString(IPreferenceConstants.PHP_USERDEF_XMLFILE));
+               if (!buffer.equals("") || buffer == null) {
+                       readFromFile(buffer);
+               }
+       }
 
-  private static String getAttributeValue(NamedNodeMap attributes, String name) {
-    Node node = attributes.getNamedItem(name);
-    return node == null ? null : node.getNodeValue();
-  }
+       public static Vector getUserDefinitions() {
+               return (Vector) userdefsyntaxdata.clone();
+       }
 
-  public static void saveToFile(File file) throws CoreException {
-    OutputStream stream = null;
-    try {
-      stream = new FileOutputStream(file);
-      saveToStream(stream);
-    } catch (IOException e) {
-      throwWriteException(e);
-    } finally {
-      try {
-        if (stream != null)
-          stream.close();
-      } catch (IOException e) {
-      }
-    }
-  }
+       private static File getSyntaxFile() {
+               IPath path = PHPeclipsePlugin.getDefault().getStateLocation();
+               path = path.append(PHPSYNTAX_FILE);
+               return path.toFile();
+       }
 
-  public static void saveToStream(OutputStream stream) throws CoreException {
-    try {
-      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
-      DocumentBuilder builder = factory.newDocumentBuilder();
-      Document document = builder.newDocument();
-      Node root = document.createElement("PHPStandardSyntax"); // $NON-NLS-1$ //$NON-NLS-1$
-      document.appendChild(root);
-      for (int i = 0; i != syntaxdata.size(); i++) {
-        Object bufferobj = (Object) syntaxdata.get(i);
-        Attr name = null;
-        Node node = document.createElement(PHPSYNTAX_TAG); // $NON-NLS-1$ //$NON-NLS-1$
-        root.appendChild(node);
-        NamedNodeMap attributes = node.getAttributes();
-        if (bufferobj instanceof PHPType)
-          name = document.createAttribute(TYPE_ATTR);
-        if (bufferobj instanceof PHPKeyword)
-          name = document.createAttribute(KEYWORD_ATTR);
-        if (bufferobj instanceof PHPFunction)
-          name = document.createAttribute(FN_ATTR);
-        if (bufferobj instanceof PHPConstant)
-          name = document.createAttribute(CONSTANT_ATTR);
-        name.setValue(((PHPElement) bufferobj).getName());
-        attributes.setNamedItem(name);
-        Attr description = document.createAttribute(USAGE_ATTR);
-        description.setValue(((PHPElement) bufferobj).getUsage());
-        attributes.setNamedItem(description);
-        if (bufferobj instanceof PHPKeyword) {
-          Attr tokenval = document.createAttribute(TOKENVAL_ATTR);
-          tokenval.setValue((new Integer(((PHPKeyword) bufferobj).gettokenval())).toString());
-          attributes.setNamedItem(tokenval);
-        }
-        if (bufferobj instanceof PHPFunction) {
-    //      Attr usage = document.createAttribute(USAGE_ATTR);
-          Text usage = document.createTextNode(((PHPFunction) bufferobj).getDescription());
-          node.appendChild(usage);
-//          usage.setValue(((PHPFunction) bufferobj).getUsage());
-//          attributes.setNamedItem(usage);
-        }
-      }
-      OutputFormat format = new OutputFormat();
-      format.setPreserveSpace(true);
-      try {
-        Serializer serializer = SerializerFactory.getSerializerFactory("xml").makeSerializer(stream, format);
-        serializer.asDOMSerializer().serialize(document);
-      } catch (UnsupportedEncodingException e) {
-      } catch (IOException e) {
-      } //$NON-NLS-1$
-      //                       Serializer serializer = SerializerFactory.getSerializer().makeSerializer(stream, format); //$NON-NLS-1$
-    } catch (ParserConfigurationException e) {
-      throwWriteException(e);
-      //        } catch (IOException e) {
-      //            throwWriteException(e);
-    }
-  }
+       private static String getAttributeValue(
+               NamedNodeMap attributes,
+               String name) {
+               Node node = attributes.getNamedItem(name);
+               return node == null ? null : node.getNodeValue();
+       }
 
-  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);
-  }
+       public static void saveToFile(File file) throws CoreException {
+               OutputStream stream = null;
+               try {
+                       stream = new FileOutputStream(file);
+                       saveToStream(stream);
+               } catch (IOException e) {
+                       throwWriteException(e);
+               } finally {
+                       try {
+                               if (stream != null)
+                                       stream.close();
+                       } catch (IOException e) {
+                       }
+               }
+       }
 
-  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);
-  }
+       public static void saveToStream(OutputStream stream) throws CoreException {
+               try {
+                       DocumentBuilderFactory factory =
+                               DocumentBuilderFactory.newInstance();
+                       DocumentBuilder builder = factory.newDocumentBuilder();
+                       Document document = builder.newDocument();
+                       Node root = document.createElement("PHPStandardSyntax"); // $NON-NLS-1$ //$NON-NLS-1$
+                       document.appendChild(root);
+                       for (int i = 0; i != syntaxdata.size(); i++) {
+                               Object bufferobj = (Object) syntaxdata.get(i);
+                               Attr name = null;
+                               Node node = document.createElement(PHPSYNTAX_TAG); // $NON-NLS-1$ //$NON-NLS-1$
+                               root.appendChild(node);
+                               NamedNodeMap attributes = node.getAttributes();
+                               if (bufferobj instanceof PHPType)
+                                       name = document.createAttribute(TYPE_ATTR);
+                               if (bufferobj instanceof PHPKeyword)
+                                       name = document.createAttribute(KEYWORD_ATTR);
+                               if (bufferobj instanceof PHPFunction)
+                                       name = document.createAttribute(FN_ATTR);
+                               if (bufferobj instanceof PHPConstant)
+                                       name = document.createAttribute(CONSTANT_ATTR);
+                               name.setValue(((PHPElement) bufferobj).getName());
+                               attributes.setNamedItem(name);
+                               Attr description = document.createAttribute(USAGE_ATTR);
+                               description.setValue(((PHPElement) bufferobj).getUsage());
+                               attributes.setNamedItem(description);
+                               if (bufferobj instanceof PHPKeyword) {
+                                       Attr tokenval = document.createAttribute(TOKENVAL_ATTR);
+                                       tokenval.setValue(
+                                               (new Integer(((PHPKeyword) bufferobj).gettokenval()))
+                                                       .toString());
+                                       attributes.setNamedItem(tokenval);
+                               }
+                               if (bufferobj instanceof PHPFunction) {
+                                       //      Attr usage = document.createAttribute(USAGE_ATTR);
+                                       Text usage =
+                                               document.createTextNode(
+                                                       ((PHPFunction) bufferobj).getDescription());
+                                       node.appendChild(usage);
+                                       //          usage.setValue(((PHPFunction) bufferobj).getUsage());
+                                       //          attributes.setNamedItem(usage);
+                               }
+                       }
+                       OutputFormat format = new OutputFormat();
+                       format.setPreserveSpace(true);
+                       try {
+                               Serializer serializer =
+                                       SerializerFactory.getSerializerFactory(
+                                               "xml").makeSerializer(
+                                               stream,
+                                               format);
+                               serializer.asDOMSerializer().serialize(document);
+                       } catch (UnsupportedEncodingException e) {
+                       } catch (IOException e) {
+                       } //$NON-NLS-1$
+                       //                      Serializer serializer = SerializerFactory.getSerializer().makeSerializer(stream, format); //$NON-NLS-1$
+               } catch (ParserConfigurationException e) {
+                       throwWriteException(e);
+                       //        } catch (IOException e) {
+                       //            throwWriteException(e);
+               }
+       }
+
+       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);
+       }
 
 }