1 22 package org.jboss.xb.binding; 23 24 import java.io.StringWriter ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.ArrayList ; 28 import org.jboss.logging.Logger; 29 import org.xml.sax.Attributes ; 30 import org.xml.sax.ContentHandler ; 31 import org.xml.sax.SAXException ; 32 33 43 public class Content 44 { 45 private static Logger log = Logger.getLogger(Content.class); 46 47 private List content = new ArrayList (); 48 private final boolean trace = log.isTraceEnabled(); 49 50 52 public String getChildContent(String namespaceURI, String qName) 53 { 54 StartElement current = ((StartElement)content.get(index)); 55 56 boolean lookingForStart = true; 57 StartElement start = null; 58 boolean childFound = false; 59 StringBuffer value = new StringBuffer (); 60 61 int i = index + 1; 62 Object next = content.get(i++); 63 while(!current.isMyEnd(next)) 64 { 65 if(lookingForStart) 66 { 67 if(next instanceof StartElement) 68 { 69 start = (StartElement)next; 70 lookingForStart = false; 71 72 if(qName.equals(start.qName) && 73 (namespaceURI == null ? start.namespaceURI == null : namespaceURI.equals(start.namespaceURI))) 74 { 75 childFound = true; 76 } 77 } 78 } 79 else if(next instanceof EndElement) 80 { 81 if(start.isMyEnd(next)) 82 { 83 if(childFound) 84 { 85 break; 86 } 87 else 88 { 89 lookingForStart = true; 90 } 91 } 92 } 93 else if(childFound && next instanceof Characters) 94 { 95 Characters chars = (Characters)next; 96 value.append(chars.ch, chars.start, chars.length); 97 } 98 next = content.get(i++); 99 } 100 return value.toString().trim(); 101 } 102 103 public void handleContent(ContentHandler handler) throws SAXException 104 { 105 handler.startDocument(); 106 107 for(Iterator i = content.iterator(); i.hasNext();) 108 { 109 Object item = i.next(); 110 if(item instanceof StartElement) 111 { 112 StartElement start = (StartElement)item; 113 handler.startElement(start.namespaceURI, start.localName, start.qName, start.attrs); 114 } 115 else if(item instanceof EndElement) 116 { 117 EndElement end = (EndElement)item; 118 handler.endElement(end.namespaceURI, end.localName, end.qName); 119 } 120 else if(item instanceof Characters) 121 { 122 Characters ch = (Characters)item; 123 handler.characters(ch.ch, ch.start, ch.length); 124 } 125 else if(item instanceof StartPrefixMapping) 126 { 127 134 } 135 else if(item instanceof EndPrefixMapping) 136 { 137 144 } 145 else 146 { 147 throw new IllegalStateException ("Unexpected element type: " + item); 148 } 149 } 150 151 handler.endDocument(); 152 } 153 154 public String toString() 155 { 156 StringWriter writer = new StringWriter (); 157 try 158 { 159 ContentWriter contentWriter = new ContentWriter(writer, true); 160 handleContent(contentWriter); 161 } 162 catch(SAXException e) 163 { 164 writer.write(e.getMessage()); 165 } 166 return writer.getBuffer().toString(); 167 } 168 169 171 public void startDocument() 172 { 173 content.clear(); 174 } 175 176 public void endDocument() 177 { 178 } 179 180 public void startPrefixMapping(String prefix, String uri) 181 { 182 StartPrefixMapping node = new StartPrefixMapping(prefix, uri); 183 content.add(node); 184 } 185 186 public void endPrefixMapping(String prefix) 187 { 188 EndPrefixMapping node = new EndPrefixMapping(prefix); 189 content.add(node); 190 } 191 192 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) 193 { 194 StartElement startElement = new StartElement(namespaceURI, localName, qName, atts); 195 content.add(startElement); 196 197 if(trace) 198 { 199 log.trace("startElement> uri=" + namespaceURI + ", local=" + localName + ", qn=" + qName + ", attrs=" + atts); 200 } 201 } 202 203 public void endElement(String namespaceURI, String localName, String qName) 204 { 205 EndElement endElement = new EndElement(namespaceURI, localName, qName); 206 content.add(endElement); 207 208 if(trace) 209 { 210 log.trace("endElement> uri=" + namespaceURI + ", local=" + localName + ", qn=" + qName); 211 } 212 } 213 214 public void characters(char[] ch, int start, int length) 215 { 216 Characters characters = new Characters(ch, start, length); 217 if(characters.toString().trim().length() > 0) 219 { 220 content.add(characters); 221 222 if(trace) 223 { 224 log.trace("characters> " + characters); 225 } 226 } 227 } 228 229 231 private int index; 232 233 public void append(Content content) 234 { 235 for(Iterator i = content.content.iterator(); i.hasNext();) 236 { 237 this.content.add(i.next()); 238 } 239 } 240 241 243 private static interface Node 244 { 245 } 246 247 public static class Characters 248 implements Node 249 { 250 private final char[] ch; 251 private final int start; 252 private final int length; 253 254 public Characters(char[] ch, int start, int length) 255 { 256 261 this.ch = new char[length]; 262 System.arraycopy(ch, start, this.ch, 0, length); 263 this.start = 0; 264 this.length = length; 265 } 266 267 public String toString() 268 { 269 return String.valueOf(ch, start, length); 270 } 271 } 272 273 public static class EndElement 274 implements Node 275 { 276 private final String namespaceURI; 277 private final String localName; 278 private final String qName; 279 280 public EndElement(String namespaceURI, String localName, String qName) 281 { 282 this.namespaceURI = namespaceURI; 283 this.localName = localName; 284 this.qName = qName; 285 } 286 287 public String toString() 288 { 289 return '[' + namespaceURI + ',' + localName + ',' + qName + ']'; 290 } 291 } 292 293 public static class StartElement 294 implements Node 295 { 296 private final String namespaceURI; 297 private final String localName; 298 private final String qName; 299 private final Attributes attrs; 300 301 public StartElement(String namespaceURI, String localName, String qName, Attributes attrs) 302 { 303 this.namespaceURI = namespaceURI; 304 this.localName = localName; 305 this.qName = qName; 306 this.attrs = new AttributesImpl(attrs); 307 } 308 309 public boolean isMyEnd(Object element) 310 { 311 boolean itis = false; 312 if(element instanceof EndElement) 313 { 314 EndElement end = (EndElement)element; 315 itis = (namespaceURI == null ? end.namespaceURI == null : namespaceURI.equals(end.namespaceURI)) 316 && qName.equals(end.qName); 317 } 318 return itis; 319 } 320 321 public String toString() 322 { 323 return '[' + namespaceURI + ',' + localName + ',' + qName + ']'; 324 } 325 } 326 327 public static class StartPrefixMapping 328 implements Node 329 { 330 public final String prefix; 331 public final String uri; 332 333 public StartPrefixMapping(String prefix, String uri) 334 { 335 this.prefix = prefix; 336 this.uri = uri; 337 } 338 339 public void read(ObjectModelBuilder builder) 340 { 341 builder.startPrefixMapping(prefix, uri); 342 } 343 344 public boolean equals(Object o) 345 { 346 if(this == o) 347 { 348 return true; 349 } 350 if(!(o instanceof StartPrefixMapping)) 351 { 352 return false; 353 } 354 355 final StartPrefixMapping startPrefixMapping = (StartPrefixMapping)o; 356 357 if(prefix != null ? !prefix.equals(startPrefixMapping.prefix) : startPrefixMapping.prefix != null) 358 { 359 return false; 360 } 361 if(uri != null ? !uri.equals(startPrefixMapping.uri) : startPrefixMapping.uri != null) 362 { 363 return false; 364 } 365 366 return true; 367 } 368 369 public int hashCode() 370 { 371 int result; 372 result = (prefix != null ? prefix.hashCode() : 0); 373 result = 29 * result + (uri != null ? uri.hashCode() : 0); 374 return result; 375 } 376 } 377 378 public static class EndPrefixMapping 379 implements Node 380 { 381 public final String prefix; 382 383 public EndPrefixMapping(String prefix) 384 { 385 this.prefix = prefix; 386 } 387 388 public void read(ObjectModelBuilder builder) 389 { 390 builder.endPrefixMapping(prefix); 391 } 392 } 393 } 394 | Popular Tags |