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.io.UnsupportedEncodingException;
 
  10 import java.util.Vector;
 
  12 import javax.xml.parsers.DocumentBuilder;
 
  13 import javax.xml.parsers.DocumentBuilderFactory;
 
  14 import javax.xml.parsers.ParserConfigurationException;
 
  16 import net.sourceforge.phpeclipse.IPreferenceConstants;
 
  17 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 
  18 import net.sourceforge.phpeclipse.phpeditor.php.PHPConstant;
 
  19 import net.sourceforge.phpeclipse.phpeditor.php.PHPElement;
 
  20 import net.sourceforge.phpeclipse.phpeditor.php.PHPFunction;
 
  21 import net.sourceforge.phpeclipse.phpeditor.php.PHPKeyword;
 
  22 import net.sourceforge.phpeclipse.phpeditor.php.PHPType;
 
  23 import org.apache.xml.serialize.OutputFormat;
 
  24 import org.apache.xml.serialize.Serializer;
 
  25 import org.apache.xml.serialize.SerializerFactory;
 
  26 import org.eclipse.core.runtime.CoreException;
 
  27 import org.eclipse.core.runtime.IPath;
 
  28 import org.eclipse.jface.preference.IPreferenceStore;
 
  29 import org.w3c.dom.Attr;
 
  30 import org.w3c.dom.Document;
 
  31 import org.w3c.dom.NamedNodeMap;
 
  32 import org.w3c.dom.Node;
 
  33 import org.w3c.dom.NodeList;
 
  34 import org.w3c.dom.Text;
 
  35 import org.xml.sax.InputSource;
 
  36 import org.xml.sax.SAXException;
 
  39  * <code>PHPSyntaxRdr</code> reads PHP specifics from an XML file (eg. keywords) 
 
  42 public class PHPSyntaxRdr {
 
  43   private static final String PHPDEFAULT_FILE = "default-phpsyntax.xml"; //$NON-NLS-1$
 
  44   private static final String PHPSYNTAX_FILE = "phpsyntax.xml"; //$NON-NLS-1$
 
  45   private static final String USERSYNTAX_FILE = "usersyntax.xml"; //$NON-NLS-1$
 
  46   private static final String USERDEFAULT_FILE = "default-usersyntax.xml"; //$NON-NLS-1$
 
  47   private static final String PHPSYNTAX_TAG = "phpsyntax"; //$NON-NLS-1$
 
  48   private static final String KEYWORD_ATTR = "keyword"; //$NON-NLS-1$
 
  49   private static final String TYPE_ATTR = "type"; //$NON-NLS-1$
 
  50   private static final String CONSTANT_ATTR = "constant"; //$NON-NLS-1$
 
  51   private static final String FN_ATTR = "function"; //$NON-NLS-1$
 
  52   private static final String USAGE_ATTR = "usage"; //$NON-NLS-1$
 
  53   private static final String TOKENVAL_ATTR = "tokenval"; //$NON-NLS-1$
 
  54   private static IPreferenceStore store;
 
  55   private static boolean hasXMLFileBeenRead = true;
 
  57   //The following variable is used to hold the syntax from
 
  58   //the suers custom file - if that file should be changed,
 
  59   //then all entries in this variable should be removed from
 
  60   //the word list, reread from the file and then reinserted.
 
  61   private static Vector userdefsyntaxdata;
 
  63   private static Vector syntaxdata;
 
  65   public PHPSyntaxRdr() {
 
  66     syntaxdata = new Vector();
 
  67     store = PHPeclipsePlugin.getDefault().getPreferenceStore();
 
  70   public static void readInSyntax() {
 
  72       hasXMLFileBeenRead = true;
 
  73       /*Attempt to read the syntax file from the metadata
 
  74        * if this does not work, create metadata from default*/
 
  75       File syntaxFile = getSyntaxFile();
 
  76       if (syntaxFile.exists()) {
 
  77         readFromFile(syntaxFile);
 
  79         readFromStream(PHPSyntaxRdr.class.getResourceAsStream(PHPSYNTAX_FILE));
 
  80         saveToFile(syntaxFile);
 
  82       /*Read the user-defined syntax file if it exists*/
 
  83       //String buffer = new String(store.getString(PHPeclipsePlugin.PHP_USERDEF_XMLFILE));
 
  85         store = PHPeclipsePlugin.getDefault().getPreferenceStore();
 
  86       String buffer = new String(store.getString(IPreferenceConstants.PHP_USERDEF_XMLFILE));
 
  87       if (!buffer.equals("") || buffer != null) {
 
  90     } catch (CoreException ce) {
 
  95   public static void readFromFile(String filename) {
 
  97       readFromFile(new File(filename));
 
  98     } catch (CoreException e) {
 
 102   public static void readFromFile(File file) throws CoreException {
 
 103     InputStream stream = null;
 
 105       stream = new FileInputStream(file);
 
 106       readFromStream(stream);
 
 107     } catch (IOException e) {
 
 108       throwReadException(e);
 
 111         if (stream != null) {
 
 114       } catch (IOException e) {
 
 118   public static void readFromStream(InputStream stream) throws CoreException {
 
 120       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 
 121       DocumentBuilder parser = factory.newDocumentBuilder();
 
 122       Document document = parser.parse(new InputSource(stream));
 
 123       //Read in the Standard PHPSyntax "stuff"
 
 124       NodeList elements = document.getElementsByTagName(PHPSYNTAX_TAG);
 
 126       int count = elements.getLength();
 
 127       for (int i = 0; i != count; i++) {
 
 128         Node node = elements.item(i);
 
 129         NamedNodeMap attributes = node.getAttributes();
 
 131         if (attributes == null)
 
 134         String Keyword = getAttributeValue(attributes, KEYWORD_ATTR);
 
 135         String Type = getAttributeValue(attributes, TYPE_ATTR);
 
 136         String Function = getAttributeValue(attributes, FN_ATTR);
 
 137         String Constant = getAttributeValue(attributes, CONSTANT_ATTR);
 
 138         String usage = getAttributeValue(attributes, USAGE_ATTR);
 
 139         String Tokenval = getAttributeValue(attributes, TOKENVAL_ATTR);
 
 141         StringBuffer buffer= new StringBuffer();
 
 142         NodeList children= node.getChildNodes();
 
 143         for (int j= 0; j != children.getLength(); j++) {
 
 144           String value= children.item(j).getNodeValue();
 
 146             buffer.append(value);
 
 148         String description = buffer.toString().trim();
 
 150         if (Keyword == null && Type == null && Function == null && Constant == null) {
 
 151           //ignore as it is not a valid phpsyntax tag
 
 153           if (Keyword != null) {
 
 154             syntaxdata.addElement(new PHPKeyword(Keyword, usage, Tokenval));
 
 155           } else if (Type != null) {
 
 156             syntaxdata.addElement(new PHPType(Type, usage));
 
 157           } else if (Function != null) {
 
 158             syntaxdata.addElement(new PHPFunction(Function, usage, description));
 
 159           } else if (Constant != null) {
 
 160             syntaxdata.addElement(new PHPConstant(Constant, usage));
 
 164     } catch (ParserConfigurationException e) {
 
 165       throwReadException(e);
 
 166     } catch (IOException e) {
 
 167       throwReadException(e);
 
 168     } catch (SAXException e) {
 
 169       throwReadException(e);
 
 173   public static Vector getsyntaxdata() {
 
 174     return (Vector) syntaxdata.clone();
 
 177   public static void replaceUserDefFile() {
 
 178     /*Replace the user-defined syntax file if it exists*/
 
 179     String buffer = new String(store.getString(IPreferenceConstants.PHP_USERDEF_XMLFILE));
 
 180     if (!buffer.equals("") || buffer == null) {
 
 181       readFromFile(buffer);
 
 185   public static Vector getUserDefinitions() {
 
 186     return (Vector) userdefsyntaxdata.clone();
 
 189   private static File getSyntaxFile() {
 
 190     IPath path = PHPeclipsePlugin.getDefault().getStateLocation();
 
 191     path = path.append(PHPSYNTAX_FILE);
 
 192     return path.toFile();
 
 195   private static String getAttributeValue(NamedNodeMap attributes, String name) {
 
 196     Node node = attributes.getNamedItem(name);
 
 197     return node == null ? null : node.getNodeValue();
 
 200   public static void saveToFile(File file) throws CoreException {
 
 201     OutputStream stream = null;
 
 203       stream = new FileOutputStream(file);
 
 204       saveToStream(stream);
 
 205     } catch (IOException e) {
 
 206       throwWriteException(e);
 
 211       } catch (IOException e) {
 
 216   public static void saveToStream(OutputStream stream) throws CoreException {
 
 218       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 
 219       DocumentBuilder builder = factory.newDocumentBuilder();
 
 220       Document document = builder.newDocument();
 
 221       Node root = document.createElement("PHPStandardSyntax"); // $NON-NLS-1$ //$NON-NLS-1$
 
 222       document.appendChild(root);
 
 223       for (int i = 0; i != syntaxdata.size(); i++) {
 
 224         Object bufferobj = (Object) syntaxdata.get(i);
 
 226         Node node = document.createElement(PHPSYNTAX_TAG); // $NON-NLS-1$ //$NON-NLS-1$
 
 227         root.appendChild(node);
 
 228         NamedNodeMap attributes = node.getAttributes();
 
 229         if (bufferobj instanceof PHPType)
 
 230           name = document.createAttribute(TYPE_ATTR);
 
 231         if (bufferobj instanceof PHPKeyword)
 
 232           name = document.createAttribute(KEYWORD_ATTR);
 
 233         if (bufferobj instanceof PHPFunction)
 
 234           name = document.createAttribute(FN_ATTR);
 
 235         if (bufferobj instanceof PHPConstant)
 
 236           name = document.createAttribute(CONSTANT_ATTR);
 
 237         name.setValue(((PHPElement) bufferobj).getName());
 
 238         attributes.setNamedItem(name);
 
 239         Attr description = document.createAttribute(USAGE_ATTR);
 
 240         description.setValue(((PHPElement) bufferobj).getUsage());
 
 241         attributes.setNamedItem(description);
 
 242         if (bufferobj instanceof PHPKeyword) {
 
 243           Attr tokenval = document.createAttribute(TOKENVAL_ATTR);
 
 244           tokenval.setValue((new Integer(((PHPKeyword) bufferobj).gettokenval())).toString());
 
 245           attributes.setNamedItem(tokenval);
 
 247         if (bufferobj instanceof PHPFunction) {
 
 248     //      Attr usage = document.createAttribute(USAGE_ATTR);
 
 249           Text usage = document.createTextNode(((PHPFunction) bufferobj).getDescription());
 
 250           node.appendChild(usage);
 
 251 //          usage.setValue(((PHPFunction) bufferobj).getUsage());
 
 252 //          attributes.setNamedItem(usage);
 
 255       OutputFormat format = new OutputFormat();
 
 256       format.setPreserveSpace(true);
 
 258         Serializer serializer = SerializerFactory.getSerializerFactory("xml").makeSerializer(stream, format);
 
 259         serializer.asDOMSerializer().serialize(document);
 
 260       } catch (UnsupportedEncodingException e) {
 
 261       } catch (IOException e) {
 
 263       //                        Serializer serializer = SerializerFactory.getSerializer().makeSerializer(stream, format); //$NON-NLS-1$
 
 264     } catch (ParserConfigurationException e) {
 
 265       throwWriteException(e);
 
 266       //        } catch (IOException e) {
 
 267       //            throwWriteException(e);
 
 271   private static void throwReadException(Throwable t) throws CoreException {
 
 272     PHPeclipsePlugin.log(t);
 
 273     //          IStatus status= new JavaUIStatus(JavaStatusConstants.TEMPLATE_IO_EXCEPTION,
 
 274     //                  TemplateMessages.getString("TemplateSet.error.read"), t); //$NON-NLS-1$
 
 275     //          throw new JavaUIException(status);
 
 278   private static void throwWriteException(Throwable t) throws CoreException {
 
 279     PHPeclipsePlugin.log(t);
 
 280     //          IStatus status= new JavaUIStatus(JavaStatusConstants.TEMPLATE_IO_EXCEPTION,
 
 281     //                  TemplateMessages.getString("TemplateSet.error.write"), t); //$NON-NLS-1$
 
 282     //          throw new JavaUIException(status);