SimpleOpenMLParser.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
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 java.util.Vector;
00029 
00030 
00039 public class SimpleOpenMLParser {
00040 
00041   public SimpleOpenMLParser() {
00042   }
00043 
00047   public static ElementData[] parseXML(String xml) {
00048     Vector list = new Vector(5);
00049  //   try {
00050       int c;
00051       boolean inside = false;
00052       boolean exclamation = false;
00053       int nonParsedTags = 0;
00054       StringBuffer element = new StringBuffer();
00055       StringBuffer pcdata = new StringBuffer();
00056       java.io.ByteArrayInputStream inputXML = new java.io.ByteArrayInputStream(xml.getBytes());
00057       while ((c = inputXML.read())> -1) {
00058         switch ((char) c) {
00059           case '!' :
00060               if (inside)  // manage CDATA and comments
00061                 exclamation = true;
00062               break;
00063           case '<' :
00064               if (inside && !exclamation)
00065                 return null;
00066                 //throw new CmlException ("Malformed document, expected '>' and found '<' during openML parsing");
00067               else if (inside && exclamation) { // suppose it is a CDATA
00068                 nonParsedTags++;
00069                 element.append((char)c);
00070               } else {
00071                 inside = true;
00072                 list.add(new ElementData(element.toString(), pcdata.toString()));
00073                 element = new StringBuffer();
00074             //    parsePcData(pcdata);
00075                 }
00076               break;
00077           case '>' :
00078               if (nonParsedTags > 0) {// suppose it is inside a CDATA
00079                 nonParsedTags--;
00080                 element.append((char)c);
00081               } else if (inside) {
00082                   if (exclamation)
00083                     exclamation = false;
00084                   inside = false;
00085               //    parseElement(element);
00086                   // ready to parse the possible PCDATA for the current element
00087                   pcdata = new StringBuffer();
00088               } else
00089                 return null;
00090                 //throw new CmlException ("Malformed document, expected '>' and found '<' during openML parsing");
00091               break;
00092          default :
00093             if (inside)
00094               element.append((char)c);
00095             else
00096               pcdata.append((char)c);
00097             break;
00098         } // end switch
00099       } // end while
00100 
00101       ElementData[] retData = new ElementData[list.size()];
00102       list.toArray(retData);
00103       return retData;
00104 /*    } catch (OpenMSPException e) {
00105         org.openmobileis.common.util.log.LogManager.traceError(org.openmobileis.common.util.log.LogServices.SYNCHROSERVICE, e);
00106       return null;
00107     }*/
00108   }
00109 }

Generated on Mon Dec 4 11:03:30 2006 for OpenMobileIS by  doxygen 1.5.1-p1