1 7 package org.jboss.xml.binding; 8 9 import org.xml.sax.Attributes ; 10 import org.xml.sax.ContentHandler ; 11 import org.xml.sax.SAXException ; 12 import org.jboss.logging.Logger; 13 14 import java.util.LinkedList ; 15 import java.io.StringWriter ; 16 17 27 public class Content 28 { 29 private static Logger log = Logger.getLogger(Content.class); 30 31 private LinkedList content = new LinkedList (); 32 33 35 public String getChildContent(String namespaceURI, String qName) 36 { 37 StartElement current = ((StartElement)content.get(index)); 38 39 boolean lookingForStart = true; 40 StartElement start = null; 41 boolean childFound = false; 42 StringBuffer value = new StringBuffer (); 43 44 int i = index + 1; 45 Object next = content.get(i++); 46 while(!current.isMyEnd(next)) 47 { 48 if(lookingForStart) 49 { 50 if(next instanceof StartElement) 51 { 52 start = (StartElement)next; 53 lookingForStart = false; 54 55 if(qName.equals(start.qName) && 56 (namespaceURI == null ? start.namespaceURI == null : namespaceURI.equals(start.namespaceURI))) 57 { 58 childFound = true; 59 } 60 } 61 } 62 else if(next instanceof EndElement) 63 { 64 if(start.isMyEnd(next)) 65 { 66 if(childFound) 67 { 68 break; 69 } 70 else 71 { 72 lookingForStart = true; 73 } 74 } 75 } 76 else if(childFound && next instanceof Characters) 77 { 78 Characters chars = (Characters)next; 79 value.append(chars.ch, chars.start, chars.length); 80 } 81 next = content.get(i++); 82 } 83 return value.toString().trim(); 84 } 85 86 public void handleContent(ContentHandler handler) throws SAXException 87 { 88 handler.startDocument(); 89 90 for(int i = 0; i < content.size(); ++i) 91 { 92 Object item = content.get(i); 93 if(item instanceof StartElement) 94 { 95 StartElement start = (StartElement)item; 96 handler.startElement(start.namespaceURI, start.localName, start.qName, start.attrs); 97 } 98 else if(item instanceof EndElement) 99 { 100 EndElement end = (EndElement)item; 101 handler.endElement(end.namespaceURI, end.localName, end.qName); 102 } 103 else if(item instanceof Characters) 104 { 105 Characters ch = (Characters)item; 106 handler.characters(ch.ch, ch.start, ch.length); 107 } 108 else 109 { 110 throw new IllegalStateException ("Unexpected element type: " + item); 111 } 112 } 113 114 handler.endDocument(); 115 } 116 117 public String toString() 118 { 119 StringWriter writer = new StringWriter (); 120 try 121 { 122 ContentWriter contentWriter = new ContentWriter(writer, true); 123 handleContent(contentWriter); 124 } 125 catch(SAXException e) 126 { 127 writer.write(e.getMessage()); 128 } 129 return writer.getBuffer().toString(); 130 } 131 132 134 public void startDocument() 135 { 136 content.clear(); 137 } 138 139 public void endDocument() 140 { 141 } 142 143 public void startPrefixMapping(String prefix, String uri) 144 { 145 StartPrefixMapping node = new StartPrefixMapping(prefix, uri); 146 content.addLast(node); 147 } 148 149 public void endPrefixMapping(String prefix) 150 { 151 EndPrefixMapping node = new EndPrefixMapping(prefix); 152 content.addLast(node); 153 } 154 155 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) 156 { 157 StartElement startElement = new StartElement(namespaceURI, localName, qName, atts); 158 content.addLast(startElement); 159 160 if(log.isTraceEnabled()) 161 { 162 log.trace("startElement> uri=" + namespaceURI + ", local=" + localName + ", qn=" + qName + ", attrs=" + atts); 163 } 164 } 165 166 public void endElement(String namespaceURI, String localName, String qName) 167 { 168 EndElement endElement = new EndElement(namespaceURI, localName, qName); 169 content.addLast(endElement); 170 171 if(log.isTraceEnabled()) 172 { 173 log.trace("endElement> uri=" + namespaceURI + ", local=" + localName + ", qn=" + qName); 174 } 175 } 176 177 public void characters(char[] ch, int start, int length) 178 { 179 Characters characters = new Characters(ch, start, length); 180 if (characters.toString().trim().length() > 0) 182 { 183 content.addLast(characters); 184 185 if(log.isTraceEnabled()) 186 { 187 log.trace("characters> " + characters); 188 } 189 } 190 } 191 192 194 private int index; 195 196 public void append(Content content) 197 { 198 for(int i = 0; i < content.content.size(); ++i) 199 { 200 this.content.addLast(content.content.get(i)); 201 } 202 } 203 204 206 private static interface Node 207 { 208 } 209 210 public static class Characters 211 implements Node 212 { 213 private final char[] ch; 214 private final int start; 215 private final int length; 216 217 public Characters(char[] ch, int start, int length) 218 { 219 224 this.ch = new char[length]; 225 System.arraycopy(ch, start, this.ch, 0, length); 226 this.start = 0; 227 this.length = length; 228 } 229 230 public String toString() 231 { 232 return String.valueOf(ch, start, length); 233 } 234 } 235 236 public static class EndElement 237 implements Node 238 { 239 private final String namespaceURI; 240 private final String localName; 241 private final String qName; 242 243 public EndElement(String namespaceURI, String localName, String qName) 244 { 245 this.namespaceURI = namespaceURI; 246 this.localName = localName; 247 this.qName = qName; 248 } 249 250 public String toString() 251 { 252 return '[' + namespaceURI + ',' + localName + ',' + qName + ']'; 253 } 254 } 255 256 public static class StartElement 257 implements Node 258 { 259 private final String namespaceURI; 260 private final String localName; 261 private final String qName; 262 private final Attributes attrs; 263 264 public StartElement(String namespaceURI, String localName, String qName, Attributes attrs) 265 { 266 this.namespaceURI = namespaceURI; 267 this.localName = localName; 268 this.qName = qName; 269 this.attrs = new AttributesImpl(attrs); 270 } 271 272 public boolean isMyEnd(Object element) 273 { 274 boolean itis = false; 275 if(element instanceof EndElement) 276 { 277 EndElement end = (EndElement)element; 278 itis = (namespaceURI == null ? end.namespaceURI == null : namespaceURI.equals(end.namespaceURI)) 279 && qName.equals(end.qName); 280 } 281 return itis; 282 } 283 284 public String toString() 285 { 286 return '[' + namespaceURI + ',' + localName + ',' + qName + ']'; 287 } 288 } 289 290 public static class StartPrefixMapping implements Node 291 { 292 public final String prefix; 293 public final String uri; 294 295 public StartPrefixMapping(String prefix, String uri) 296 { 297 this.prefix = prefix; 298 this.uri = uri; 299 } 300 301 public void read(ObjectModelBuilder builder) 302 { 303 builder.startPrefixMapping(prefix, uri); 304 } 305 306 public boolean equals(Object o) 307 { 308 if(this == o) return true; 309 if(!(o instanceof StartPrefixMapping)) return false; 310 311 final StartPrefixMapping startPrefixMapping = (StartPrefixMapping) o; 312 313 if(prefix != null ? !prefix.equals(startPrefixMapping.prefix) : startPrefixMapping.prefix != null) return false; 314 if(uri != null ? !uri.equals(startPrefixMapping.uri) : startPrefixMapping.uri != null) return false; 315 316 return true; 317 } 318 319 public int hashCode() 320 { 321 int result; 322 result = (prefix != null ? prefix.hashCode() : 0); 323 result = 29 * result + (uri != null ? uri.hashCode() : 0); 324 return result; 325 } 326 } 327 328 public static class EndPrefixMapping implements Node 329 { 330 public final String prefix; 331 332 public EndPrefixMapping(String prefix) 333 { 334 this.prefix = prefix; 335 } 336 337 public void read(ObjectModelBuilder builder) 338 { 339 builder.endPrefixMapping(prefix); 340 } 341 } 342 } 343 | Popular Tags |