KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > commons > xml > ExoXPPParser


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.commons.xml;
6
7 import org.xmlpull.mxp1.MXParserCachingStrings;
8 import org.xmlpull.v1.XmlPullParser ;
9 /**
10  * Jul 8, 2004
11  * @author: Tuan Nguyen
12  * @email: tuan08@users.sourceforge.net
13  * @version: $Id: ExoXPPParser.java,v 1.4 2004/10/20 20:58:27 tuan08 Exp $
14  */

15 public class ExoXPPParser extends MXParserCachingStrings {
16   private String JavaDoc[] nodeAttributeName_ = new String JavaDoc[20];
17   private String JavaDoc[] nodeAttributeValue_ = new String JavaDoc[20];
18   private int nodeAttributeCount_ ;
19     
20   public boolean node(String JavaDoc name) throws Exception JavaDoc {
21     if(this.eventType == XmlPullParser.END_DOCUMENT) return false ;
22     while(this.eventType != XmlPullParser.START_TAG) {
23         next() ;
24       if(this.eventType == XmlPullParser.END_DOCUMENT) return false ;
25     }
26     if(getName().equals(name)) {
27       copyAttributes() ;
28         next() ;
29         return true ;
30     }
31     return false ;
32   }
33   
34   public String JavaDoc nodeContent(String JavaDoc name) throws Exception JavaDoc {
35     if(node(name)) return getContent() ;
36     return null ;
37   }
38   
39   public void endNode(String JavaDoc name) throws Exception JavaDoc {
40     while(!(this.eventType == XmlPullParser.END_TAG && name.equals(getName()))) {
41       nextTag() ;
42     }
43   }
44   
45   public void mandatoryNode(String JavaDoc name) throws Exception JavaDoc {
46     if(this.eventType == XmlPullParser.END_DOCUMENT) {
47         throw new Exception JavaDoc("expect tag name " + name + ", but end of document") ;
48     }
49     while(this.eventType != XmlPullParser.START_TAG ) {
50         next() ;
51         if(this.eventType == XmlPullParser.END_DOCUMENT) {
52             throw new Exception JavaDoc("expect tag name " + name + ", but end of document") ;
53         }
54     }
55     if(!getName().equals(name)) {
56         throw new Exception JavaDoc("expect tag name " + name + ", but find " + getName()) ;
57     }
58     copyAttributes() ;
59     next() ;
60   }
61   
62   public String JavaDoc mandatoryNodeContent(String JavaDoc name) throws Exception JavaDoc {
63     mandatoryNode(name) ;
64     return getContent() ;
65   }
66   
67     public String JavaDoc getContent() throws Exception JavaDoc {
68         if(this.eventType != TEXT) {
69             return null ; //throw new Exception("Not a text node, name : " + getName()) ;
70
}
71         return this.getText() ;
72     }
73   
74   public String JavaDoc getNodeAttributeValue(String JavaDoc name) {
75     for(int i = 0 ; i < nodeAttributeCount_; i++) {
76         if(name.equals(nodeAttributeName_[i])) return nodeAttributeValue_[i] ;
77     }
78     return null ;
79   }
80   
81   private void copyAttributes() {
82     for(int i = 0 ; i < this.attributeCount; i++ ) {
83         nodeAttributeName_[i] = this.attributeName[i] ;
84       nodeAttributeValue_[i] = this.attributeValue[i] ;
85     }
86     nodeAttributeCount_ = this.attributeCount ;
87   }
88   
89   static public ExoXPPParser getInstance() throws Exception JavaDoc {
90     return new ExoXPPParser() ;
91   }
92 }
Popular Tags