Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

Parser.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2005 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: openmobileis@e-care.fr
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  * 
00024  */
00025 
00026 package org.openmobileis.synchro.openmsp.protocol;
00027 
00028 import org.openmobileis.synchro.openmsp.OpenMSPException;
00029 
00038 public class Parser {
00039 
00040   private static int BEGIN_TAG = 0;
00041   private static int END_TAG = 1;
00042   private static int SINGLE_TAG = 2;
00043 
00044   private Handler handler = null;
00045   // Manage elements tree - maximum deep is 50
00046   private String[] elements = new String[50];
00047   private int index = 0;
00048 
00049   public Parser(Handler handler) {
00050     this.handler = handler;
00051   }
00052 
00053   public void parse (String inputCyberML) throws OpenMSPException {
00054     int c;
00055     boolean inside = false;
00056     boolean exclamation = false;
00057     boolean isPCData = false;
00058     StringBuffer element = new StringBuffer();
00059     StringBuffer pcdata = new StringBuffer();
00060     java.io.ByteArrayInputStream inputXML = new java.io.ByteArrayInputStream(inputCyberML.getBytes());
00061     while ((c = inputXML.read())> -1) {
00062         switch ((char) c) {
00063           case '!' :
00064               if (inside)  // manage CDATA and comments
00065                 exclamation = true;
00066               break;
00067           case '<' :
00068               if (inside && !exclamation)
00069                 throw new OpenMSPException ("Malformed document, expected '>' and found '<' during OpenMSP parsing");
00070               else if (inside && exclamation) { // suppose it is a CDATA
00071                 isPCData = true;
00072                 element.append((char)c);
00073               } else {
00074                 inside = true;
00075                 element = new StringBuffer();
00076                 parsePcData(pcdata);
00077                 }
00078               break;
00079           case '>' :
00080               if (isPCData) { // test if it is the end of PC data ... could be better !! (can not use stringBuffer.substring() in jdk1.1
00081                   String tempStr = element.toString().substring(element.length()-2, element.length());
00082                   if (tempStr.equals("]]")) {// suppose it is inside a CDATA
00083                       isPCData = false;
00084                       exclamation = false;
00085                   }
00086               }
00087               if ((inside) && (!isPCData)) {
00088                   inside = false;
00089                   parseElement(element);
00090                   // ready to parse the possible PCDATA for the current element
00091                   pcdata = new StringBuffer();
00092               } else if (isPCData) {
00093                   element.append((char)c);
00094               } else
00095                 throw new OpenMSPException ("Malformed document, expected '>' and found '<' during OpenMSP parsing");
00096               break;
00097          default :
00098             if (inside)
00099               element.append((char)c);
00100             else
00101               pcdata.append((char)c);
00102             break;
00103         } // end switch
00104       } // end while
00105    } //end parse
00106 
00107 
00108 // Analyse elements (header, comments, begin or end tags
00109  private void parseElement (StringBuffer element) throws OpenMSPException {
00110     String parsedElement = element.toString();
00111     char c = parsedElement.charAt(0);
00112     switch (c) {
00113        case '!' :
00114           // skip xml COMMENT
00115           index = parsedElement.indexOf("--");
00116           int lastIndex = parsedElement.indexOf("--", parsedElement.length()-2);
00117           if ((index != -1) && (lastIndex != -1)) // find comment
00118             break;
00119         case '[' :
00120           if ((parsedElement.startsWith("[CDATA[")) && (parsedElement.endsWith("]]")))   { // manage CDATA
00121             String data = parsedElement.substring("[CDATA[".length() , parsedElement.length()-2);
00122             parsePcData(new StringBuffer(data));
00123           } else // Error, malformed tag
00124             throw new OpenMSPException ("Found illegal character '!' in element : " + parsedElement);
00125           break;
00126       case '/' : // end element
00127           addElement (parsedElement.substring(1), Parser.END_TAG);
00128           break;
00129       default :
00130           // case single tag : <xxx/>
00131             if (parsedElement.endsWith("/"))
00132                 addElement (parsedElement.substring(0, parsedElement.length() -1), Parser.SINGLE_TAG);
00133             else
00134                 addElement (parsedElement, Parser.BEGIN_TAG);
00135         } // end switch
00136   }
00137 
00138 private void parsePcData (StringBuffer data) throws OpenMSPException {
00139     if ((data != null) && (data.toString().trim().length() > 0))
00140       handler.getValue (data.toString());
00141   }
00142 
00143 
00144 private void addElement (String elementName, int elementStatus) throws OpenMSPException {
00145     if (elementStatus == Parser.SINGLE_TAG) {
00146       handler.startElement(elementName);
00147       handler.endElement(elementName);
00148     } else if (elementStatus == Parser.BEGIN_TAG) {
00149       elements[index] = elementName;
00150       index++;
00151       handler.startElement(elementName);
00152     } else if (elementStatus == Parser.END_TAG) {
00153         if (!elements[index-1].equals(elementName))
00154           throw new OpenMSPException ("Malformed document, found " + elementName + "expected : " + elements[index]);
00155         else {
00156            elements[index] = null;
00157            index--;
00158            handler.endElement(elementName);
00159         }
00160     }
00161   }
00162 
00163  }

Generated on Wed Dec 14 21:05:34 2005 for OpenMobileIS by  doxygen 1.4.4