1 5 package org.exoplatform.commons.xml; 6 7 import org.xmlpull.mxp1.MXParserCachingStrings; 8 import org.xmlpull.v1.XmlPullParser ; 9 15 public class ExoXPPParser extends MXParserCachingStrings { 16 private String [] nodeAttributeName_ = new String [20]; 17 private String [] nodeAttributeValue_ = new String [20]; 18 private int nodeAttributeCount_ ; 19 20 public boolean node(String name) throws Exception { 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 nodeContent(String name) throws Exception { 35 if(node(name)) return getContent() ; 36 return null ; 37 } 38 39 public void endNode(String name) throws Exception { 40 while(!(this.eventType == XmlPullParser.END_TAG && name.equals(getName()))) { 41 nextTag() ; 42 } 43 } 44 45 public void mandatoryNode(String name) throws Exception { 46 if(this.eventType == XmlPullParser.END_DOCUMENT) { 47 throw new Exception ("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 ("expect tag name " + name + ", but end of document") ; 53 } 54 } 55 if(!getName().equals(name)) { 56 throw new Exception ("expect tag name " + name + ", but find " + getName()) ; 57 } 58 copyAttributes() ; 59 next() ; 60 } 61 62 public String mandatoryNodeContent(String name) throws Exception { 63 mandatoryNode(name) ; 64 return getContent() ; 65 } 66 67 public String getContent() throws Exception { 68 if(this.eventType != TEXT) { 69 return null ; } 71 return this.getText() ; 72 } 73 74 public String getNodeAttributeValue(String 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 { 90 return new ExoXPPParser() ; 91 } 92 } | Popular Tags |