1 16 package org.apache.axis2.om.impl.llom; 17 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.util.Date ; 21 import java.util.Random ; 22 23 import javax.activation.DataHandler ; 24 import javax.xml.stream.XMLStreamException; 25 import javax.xml.stream.XMLStreamWriter; 26 27 import org.apache.axis2.attachments.Base64; 28 import org.apache.axis2.attachments.ByteArrayDataSource; 29 import org.apache.axis2.attachments.IOUtils; 30 import org.apache.axis2.om.OMAttribute; 31 import org.apache.axis2.om.OMConstants; 32 import org.apache.axis2.om.OMElement; 33 import org.apache.axis2.om.OMException; 34 import org.apache.axis2.om.OMNamespace; 35 import org.apache.axis2.om.OMNode; 36 import org.apache.axis2.om.OMOutput; 37 import org.apache.axis2.om.OMText; 38 import org.apache.axis2.om.OMXMLParserWrapper; 39 import org.apache.axis2.om.impl.llom.mtom.MTOMStAXSOAPModelBuilder; 40 41 44 public class OMTextImpl extends OMNodeImpl implements OMText, OMConstants { 45 46 protected String value = null; 47 48 protected short textType = TEXT_NODE; 49 50 protected String mimeType; 51 52 protected boolean optimize = false; 53 54 protected boolean isBinary = false; 55 56 private static Random rnd = new Random (new Date ().getTime()); 57 58 62 private String contentID = null; 63 64 67 private DataHandler dataHandler = null; 68 69 72 protected OMNamespace ns = new OMNamespaceImpl( 73 "http://www.w3.org/2004/08/xop/Include", "xop"); 74 75 78 protected String localName = "Include"; 79 80 83 protected OMAttribute attribute; 84 85 90 public OMTextImpl(String s) { 91 this.value = s; 92 } 93 94 100 public OMTextImpl(OMElement parent, String text) { 101 super(parent); 102 this.value = text; 103 done = true; 104 } 105 106 112 public OMTextImpl(String s, String mimeType, boolean optimize) { 113 this(null,s,mimeType,optimize); 114 } 115 116 123 public OMTextImpl(OMElement parent, String s, String mimeType, 124 boolean optimize) { 125 this(parent, s); 126 this.mimeType = mimeType; 127 this.optimize = optimize; 128 if (this.contentID == null && optimize==true) { 129 createContentID(); 130 } 131 done = true; 132 } 133 134 138 public OMTextImpl(DataHandler dataHandler) { 139 this(dataHandler,true); 140 141 } 142 143 148 public OMTextImpl(DataHandler dataHandler, boolean optimize) { 149 this.dataHandler = dataHandler; 150 this.isBinary = true; 151 this.optimize = optimize; 152 if (this.contentID == null && optimize==true) { 153 createContentID(); 154 } 155 done = true; 156 157 } 158 159 167 public OMTextImpl(String contentID, OMElement parent, 168 OMXMLParserWrapper builder) { 169 super(parent); 170 this.contentID = contentID; 171 this.optimize = true; 172 this.isBinary = true; 173 this.builder = builder; 174 done = true; 175 176 } 177 178 184 public void setTextType(short type) { 185 if ((type == TEXT_NODE) || (type == COMMENT_NODE) 186 || (type == CDATA_SECTION_NODE)) { 187 this.textType = type; 188 } else { 189 throw new UnsupportedOperationException ("Attempt to set wrong type"); 190 } 191 } 192 193 public int getType() throws OMException { 194 return textType; 195 } 196 197 201 public void serializeWithCache(OMOutput omOutput) throws XMLStreamException { 202 XMLStreamWriter writer = omOutput.getXmlStreamWriter(); 203 if (textType == TEXT_NODE) { 204 writer.writeCharacters(this.value); 205 } else if (textType == COMMENT_NODE) { 206 writer.writeComment(this.value); 207 } else if (textType == CDATA_SECTION_NODE) { 208 writer.writeCData(this.value); 209 } 210 OMNode nextSibling = this.getNextSibling(); 211 if (nextSibling != null) { 212 nextSibling.serializeWithCache(omOutput); 213 } 214 } 215 216 219 public String getText() throws OMException { 220 if (this.value != null) { 221 return this.value; 222 } else { 223 try { 224 InputStream inStream; 225 inStream = this.getInputStream(); 226 byte[] data; 227 data = new byte[inStream.available()]; 228 IOUtils.readFully(inStream, data); 229 return Base64.encode(data); 230 } catch (Exception e) { 231 throw new OMException( 232 "Cannot read from Stream taken form the Data Handler" 233 + e); 234 } 235 } 236 237 } 238 239 public boolean isOptimized() { 240 return optimize; 241 } 242 243 public void doOptimize(boolean value) { 244 this.optimize = value; 245 if (this.contentID == null && value==true) { 246 getContentID(); 247 } 248 } 249 250 255 public DataHandler getDataHandler() { 256 257 261 if (value != null) { 262 ByteArrayDataSource dataSource; 263 byte[] data = Base64.decode(value); 264 if (mimeType != null) { 265 dataSource = new ByteArrayDataSource(data, mimeType); 266 } else { 267 dataSource = new ByteArrayDataSource(data); 269 } 270 DataHandler dataHandler = new DataHandler (dataSource); 271 return dataHandler; 272 } else { 273 if (dataHandler == null) { 274 dataHandler = ((MTOMStAXSOAPModelBuilder) builder) 275 .getDataHandler(contentID); 276 } 277 return dataHandler; 278 } 279 } 280 281 public String getLocalName() { 282 return localName; 283 } 284 285 public java.io.InputStream getInputStream() throws OMException { 286 if (isBinary == true) { 287 if (dataHandler == null) { 288 getDataHandler(); 289 } 290 InputStream inStream; 291 try { 292 inStream = dataHandler.getDataSource().getInputStream(); 293 } catch (IOException e) { 294 throw new OMException( 295 "Cannot get InputStream from DataHandler." + e); 296 } 297 return inStream; 298 } else { 299 throw new OMException("Unsupported Operation"); 300 } 301 } 302 303 public String getContentID() { 304 return this.contentID; 305 } 306 307 public boolean isComplete() { 308 return true; 309 } 310 311 public void serialize(OMOutput omOutput) throws XMLStreamException { 312 boolean firstElement = false; 313 314 if (!this.isBinary) { 315 serializeWithCache(omOutput); 316 } else { 317 if (omOutput.doOptimise()) { 318 this.attribute = new OMAttributeImpl("href", 320 new OMNamespaceImpl("", ""), "cid:" 321 + this.contentID.trim()); 322 323 this.serializeStartpart(omOutput); 324 omOutput.writeOptimised(this); 325 omOutput.getXmlStreamWriter().writeEndElement(); 326 } else { 327 omOutput.getXmlStreamWriter().writeCharacters(this.getText()); 328 } 329 OMNode nextSibling = this.getNextSibling(); 331 if (nextSibling != null) { 332 nextSibling.serialize(omOutput); 334 } else { 335 if (parent == null) { 337 return; 338 } else if (parent.isComplete()) { 339 return; 340 } else { 341 builder.next(); 344 } 345 346 } 347 } 348 349 } 350 private void createContentID() 351 { 352 this.contentID = "2"+String.valueOf(rnd.nextLong())+"@schemas.xmlsoap.org"; 355 } 356 357 358 361 private void serializeStartpart(OMOutput omOutput) 362 throws XMLStreamException { 363 String nameSpaceName = null; 364 String writer_prefix = null; 365 String prefix = null; 366 XMLStreamWriter writer = omOutput.getXmlStreamWriter(); 367 if (this.ns != null) { 368 nameSpaceName = this.ns.getName(); 369 writer_prefix = writer.getPrefix(nameSpaceName); 370 prefix = this.ns.getPrefix(); 371 372 if (nameSpaceName != null) { 373 if (writer_prefix != null) { 374 writer.writeStartElement(nameSpaceName, 375 this.getLocalName()); 376 } else { 377 if (prefix != null) { 378 writer.writeStartElement(prefix, this.getLocalName(), 379 nameSpaceName); 380 writer.setPrefix(prefix, nameSpaceName); 383 } else { 384 writer.writeStartElement(nameSpaceName, 385 this.getLocalName()); 386 writer.writeDefaultNamespace(nameSpaceName); 387 writer.setDefaultNamespace(nameSpaceName); 388 } 389 } 390 } else { 391 writer.writeStartElement(this.getLocalName()); 392 393 } 394 } else { 395 writer.writeStartElement(this.getLocalName()); 396 397 } 398 399 401 serializeAttribute(this.attribute, omOutput); 402 403 serializeNamespace(this.ns, omOutput); 405 406 } 407 408 415 static void serializeAttribute(OMAttribute attr, OMOutput omOutput) 416 throws XMLStreamException { 417 418 XMLStreamWriter writer = omOutput.getXmlStreamWriter(); 419 OMNamespace ns = attr.getNamespace(); 421 String prefix = null; 422 String namespaceName = null; 423 if (ns != null) { 424 425 prefix = ns.getPrefix(); 427 namespaceName = ns.getName(); 428 if (prefix != null) { 429 writer.writeAttribute(prefix, namespaceName, attr 430 .getLocalName(), attr.getValue()); 431 } else { 432 writer.writeAttribute(namespaceName, attr.getLocalName(), attr 433 .getValue()); 434 } 435 } else { 436 writer.writeAttribute(attr.getLocalName(), attr.getValue()); 437 } 438 } 439 440 447 static void serializeNamespace(OMNamespace namespace, OMOutput omOutput) 448 throws XMLStreamException { 449 XMLStreamWriter writer = omOutput.getXmlStreamWriter(); 450 if (namespace != null) { 451 String uri = namespace.getName(); 452 String ns_prefix = namespace.getPrefix(); 454 writer.writeNamespace(ns_prefix, namespace.getName()); 456 writer.setPrefix(ns_prefix, uri); 457 } 459 } 460 461 466 public void discard() throws OMException { 467 if (done) { 468 this.detach(); 469 } else { 470 builder.discard((OMElement) this.parent); 471 } 472 } 473 474 } | Popular Tags |