1 package org.exoplatform.services.xml.querying.impl.xtas; 2 3 import org.xml.sax.ContentHandler ; 4 import org.xml.sax.Attributes ; 5 import org.xml.sax.SAXException ; 6 import org.xml.sax.Locator ; 7 8 public class XTASMLContentHandler implements ContentHandler 9 { 10 private static final int ROOT = 0; 11 private static final int QUERY = 1; 12 private static final int INSTRUCTION = 2; 13 14 private int state = ROOT; 15 16 private boolean hasPrefixes = false; 17 18 private String sourceId; 19 private String destinationId; 20 21 private String match; 22 private String type; 23 private String newValue = ""; 24 25 26 public XTASMLContentHandler () 27 { 28 } 30 31 public void setDocumentLocator (Locator locator) 32 { 33 } 34 35 public void startDocument () 36 throws SAXException 37 { 38 } 39 40 public void endDocument() 41 throws SAXException 42 { 43 } 44 45 public void startPrefixMapping (String prefix, String uri) 46 throws SAXException 47 { 48 this.hasPrefixes = true; 50 System.out.println("Prefix: "+prefix+" "+uri); 51 } 52 53 54 public void endPrefixMapping (String prefix) 55 throws SAXException 56 { 57 } 58 59 public void startElement (String namespaceURI, String localName, 60 String qName, Attributes atts) 61 throws SAXException 62 { 63 66 if( state == ROOT ) { 67 if( localName.equals("query") ) { 68 69 int n = atts.getLength(); 70 for(int i = 0; i < n; i++) { 72 if( atts.getLocalName(i).equals("source") ) 73 sourceId = atts.getValue(i); 74 else if( atts.getLocalName(i).equals("destination") ) 75 destinationId = atts.getValue(i); 76 else 77 if(!hasPrefixes) 78 throw new SAXException ("Unknown query attribute '"+atts.getQName(i)+"'!"); 79 } 80 state = QUERY; 81 82 } else 83 throw new SAXException ("The XTASML's root element must be 'query'!"); 84 85 } else if( state == QUERY ) { 86 type = localName; 87 int n = atts.getLength(); 88 for(int i = 0; i < n; i++) { 90 if( atts.getLocalName(i).equals("match") || atts.getLocalName(i).equals("xpath") || atts.getLocalName(i).equals("resource")) 91 match=atts.getValue(i); 92 else 93 throw new SAXException ("Unknown query attribute '"+atts.getQName(i)+"'!"); 94 } 95 state = INSTRUCTION; 96 } else if( state == INSTRUCTION ) { 97 newValue+="<"+localName; 98 int n = atts.getLength(); 99 for(int i = 0; i < n; i++) { 100 newValue+=" "+atts.getLocalName(i)+"=\""+atts.getValue(i)+"\""; 101 } 102 newValue+=">"; 103 104 } 105 106 107 } 108 109 110 public void endElement (String namespaceURI, String localName, 111 String qName) 112 throws SAXException 113 { 114 if( localName == type ) 115 state = QUERY; 116 117 if( state == INSTRUCTION ) { 118 newValue+="</"+localName+">"; 119 } 120 121 } 122 123 124 public void characters (char[] ch, int start, int length) 125 throws SAXException 126 { 127 String s = new String (ch, start, length); 129 newValue+=s.trim(); 130 131 133 } 134 135 136 public void ignorableWhitespace (char[] ch, int start, int length) 137 throws SAXException 138 { 139 String s = new String (ch, start, length); 140 newValue+=s; 141 143 } 144 145 public void processingInstruction (String target, String data) 146 throws SAXException 147 { 148 } 149 150 public void skippedEntity (String name) 151 throws SAXException 152 { 153 } 154 155 public String getSourceId() 156 { 157 return sourceId; 158 } 159 160 public String getDestinationId() 161 { 162 return destinationId; 163 } 164 165 public String getType() 166 { 167 return type; 168 } 169 170 public String getMatch() 171 { 172 return match; 173 } 174 175 public String getNewValue() 176 { 177 return newValue; 178 } 179 } 180 | Popular Tags |