1 6 7 11 12 package org.openlaszlo.xml; 13 14 import java.io.*; 15 import java.util.*; 16 17 import org.xml.sax.*; 18 19 import org.openlaszlo.xml.internal.*; 20 import org.openlaszlo.iv.flash.util.*; 21 import org.openlaszlo.iv.flash.api.action.*; 22 import org.openlaszlo.iv.flash.api.*; 23 import org.openlaszlo.compiler.CompilationError; 24 import org.openlaszlo.utils.ChainedException; 25 import org.openlaszlo.utils.FileUtils; 26 import org.openlaszlo.utils.HashIntTable; 27 28 import org.jdom.input.SAXBuilder; 29 import org.jdom.output.SAXOutputter; 30 import org.jdom.JDOMException; 31 import org.jdom.Document; 32 import org.jdom.Attribute; 33 import org.jdom.Element; 34 import org.xml.sax.Attributes ; 35 import org.xml.sax.helpers.AttributesImpl ; 36 37 import org.apache.log4j.*; 38 39 40 79 public class DataEncoder implements org.xml.sax.ContentHandler { 80 81 82 private static Logger mLogger = Logger.getLogger(DataEncoder.class); 83 84 85 private int initsize = 0; 86 private static final int DEFAULT_BUFFER_SIZE = 4096; 87 88 91 private FlashOutput mSWF = null; 92 95 private long mSize = -1; 96 97 int mFlashVersion = 6; 99 100 103 public DataEncoder () { } 104 105 109 public DataEncoder (int initsize) { 110 this.initsize = initsize; 111 } 112 113 117 126 public void characters(char[] ch, int start, int length) { 127 String text = new String (ch, start, length); 128 characters(text); 129 } 130 131 138 public void characters(String text) { 139 body.writeByte(Actions.PushDuplicate); 143 145 146 body.writeByte(Actions.PushData); 147 int push_bufferpos = body.getPos(); 150 body.writeWord(0); 152 DataCommon.pushMergedStringData(text, body, dc); 153 body.writeByte(0x07); body.writeDWord(2); body.writeByte(0x08); body.writeByte(textnode_idx); 160 int total_size = body.getPos() - (push_bufferpos + 2); 162 body.writeWordAt(total_size, push_bufferpos); 164 165 body.writeByte(Actions.CallFunction); 166 body.writeByte(Actions.Pop); 168 } 169 170 171 184 public void endElement(java.lang.String uri, java.lang.String localName, java.lang.String qName) { 185 body.writeByte(Actions.Pop); 187 } 188 189 194 public void endElement() { 195 body.writeByte(Actions.Pop); 197 } 198 199 200 205 public void endPrefixMapping(java.lang.String prefix) { 206 } 207 208 216 public void ignorableWhitespace(char[] ch, int start, int length) { 217 } 218 219 228 public void processingInstruction(java.lang.String target, java.lang.String data) { 229 } 230 231 238 public void setDocumentLocator(Locator locator) { 239 } 240 241 248 public void skippedEntity(java.lang.String name) { 249 } 250 251 252 DataContext dc; 253 private byte constructor_idx; 255 private byte textnode_idx; 256 257 FlashBuffer body; 258 Program program; 259 Program resultProgram; 260 FlashBuffer out; 261 262 267 public void startDocument() { 268 dc = new DataContext(mFlashVersion); 269 if (mFlashVersion == 5) { 270 dc.setEncoding("Cp1252"); 271 } else { 272 dc.setEncoding("UTF-8"); 273 } 274 constructor_idx = (byte) (DataCommon.addStringConstant(DataCommon.NODE_INSTANTIATOR_FN, dc) & 0xFF); 275 textnode_idx = (byte) (DataCommon.addStringConstant(DataCommon.TEXT_INSTANTIATOR_FN, dc) & 0xFF); 276 277 body = new FlashBuffer(initsize == 0 ? DEFAULT_BUFFER_SIZE : initsize); 280 program = new Program(body); 281 282 program.push(new Object []{"_m", "_root"}); 285 program.getVar(); 286 program.push("_m"); 287 body.writeByte(Actions.GetMember); 288 program.setVar(); 289 290 program.push(new Object []{"_t", "_root"}); 292 program.getVar(); 293 program.push("_t"); 294 body.writeByte(Actions.GetMember); 295 program.setVar(); 296 297 program.push(0); program.push("_root"); 302 program.getVar(); 303 program.push(DataCommon.ROOT_NODE_INSTANTIATOR_FN); 304 program.callMethod(); 305 308 AttributesImpl emptyAttr = new AttributesImpl (); 309 startElement("resultset", emptyAttr); 310 startElement("body", emptyAttr); 311 } 312 313 318 public void endDocument() { 319 320 endElement(); 321 endElement(); 323 324 program.push(1); 327 program.push("_root"); 328 program.getVar(); 329 program.push(DataCommon.ROOT_NODE_FINAL_FN); 330 program.callMethod(); 331 332 FlashBuffer body = program.body(); 333 334 body.writeByte(Actions.PushDuplicate); 336 program.push("__lztmproot"); 337 body.writeByte(Actions.StackSwap); 338 program.setVar(); 339 340 program.push("_parent"); 342 program.getVar(); 343 program.push(2); 344 program.push("_parent"); 345 program.getVar(); 346 program.push("loader"); 347 body.writeByte(Actions.GetMember); 348 program.push("returnData"); 349 program.callMethod(); 350 program.pop(); 351 352 byte pooldata[] = DataCommon.makeStringPool(dc); 354 final int MISC = 64; 356 out = new FlashBuffer(body.getSize() + pooldata.length + MISC); 357 out.writeByte( Actions.ConstantPool ); 359 out.writeWord( pooldata.length + 2 ); out.writeWord( dc.cpool.size() ); out.writeArray( pooldata, 0, pooldata.length); out.writeArray(body.getBuf(), 0, body.getSize()); 364 resultProgram = new Program(out); 365 } 366 367 370 private Program getProgram() { 371 return resultProgram; 372 } 373 374 375 380 private FlashFile makeSWFFile() { 381 FlashFile file = FlashFile.newFlashFile(); 383 Script s = new Script(1); 384 file.setMainScript(s); 385 file.setVersion(5); 386 Frame frame = s.newFrame(); 387 frame.addFlashObject(new DoAction(resultProgram)); 388 return file; 389 } 390 391 412 413 414 422 public InputStream getInputStream() 423 throws IOException { 424 425 generate(); 426 return mSWF.getInputStream(); 427 } 428 429 436 public long getSize() 437 throws IOException { 438 439 generate(); 440 return mSize; 441 } 442 443 446 private void generate() throws IOException { 447 448 if (mSWF == null) { 449 try { 450 InputStream input; 451 FlashFile file = makeSWFFile(); 452 mSWF = file.generate(); 453 mSize = mSWF.pos; 454 } catch (IVException ex) { 455 throw new ChainedException(ex); 456 } 457 } 458 } 459 460 465 public void _startElement (String localName) { 466 467 } 468 469 473 public void addAttribute (String attrName, String attrVal) { 474 475 } 476 477 478 495 public void startElement(java.lang.String uri, java.lang.String localName, java.lang.String qName, Attributes atts) { 496 startElement(localName, atts); 497 } 498 499 500 511 public void startElement(java.lang.String localName, Attributes atts) { 512 int idx; body.writeByte(Actions.PushDuplicate); 517 518 body.writeByte(Actions.PushData); 524 int push_bufferpos = body.getPos(); 527 body.writeWord(0); 529 String eltname = localName; 531 DataCommon.pushMergedStringDataSymbol(eltname, body, dc); 532 533 int nattrs = atts.getLength(); 536 537 for (int i = 0; i < nattrs; i++) { 539 String attrname = atts.getLocalName(i); 540 DataCommon.pushMergedStringDataSymbol(attrname, body, dc); 542 543 String attrval = atts.getValue(i); 544 DataCommon.pushMergedStringData(attrval, body, dc); 546 } 547 body.writeByte(0x07); body.writeDWord(nattrs); 550 int total_size = body.getPos() - (push_bufferpos + 2); 552 body.writeWordAt(total_size, push_bufferpos); 554 body.writeByte(Actions.InitObject); 555 556 body.writeByte(Actions.PushData); 561 body.writeWord(7); 562 body.writeByte(0x07); body.writeDWord(3); body.writeByte(0x08); body.writeByte(constructor_idx); body.writeByte(Actions.CallFunction); 567 } 570 571 578 public void startPrefixMapping(java.lang.String prefix, java.lang.String uri) { 579 } 580 581 582 586 587 593 public void buildFromDocument(Document doc) throws DataEncoderException { 594 try { 595 SAXOutputter saxout = new SAXOutputter(this); 596 saxout.output(doc); 597 } 598 catch (JDOMException e) { 599 throw new DataEncoderException("Error compiling XML from Document: "+e.getMessage()); 600 } 601 } 602 603 608 public void buildFromElement(Element e) { 609 startDocument(); 610 traverseDOM(e); 611 endDocument(); 612 } 613 614 String getQualifiedName(Attribute attr) 615 { 616 619 if (attr.getNamespacePrefix().equals("")) 620 return ""; 621 return attr.getQualifiedName(); 622 } 623 624 String getAttributeType(Attribute attr) 625 { 626 629 int type = attr.getAttributeType(); 630 if ( type == Attribute.CDATA_ATTRIBUTE) { return "CDATA"; } 631 else if ( type == Attribute.ID_ATTRIBUTE) { return "ID"; } 632 else if ( type == Attribute.IDREF_ATTRIBUTE) { return "IDREF"; } 633 else if ( type == Attribute.IDREFS_ATTRIBUTE) { return "IDREFS"; } 634 else if ( type == Attribute.ENTITY_ATTRIBUTE) { return "ENTITY"; } 635 else if ( type == Attribute.ENTITIES_ATTRIBUTE) { return "ENTITIES"; } 636 else if ( type == Attribute.NMTOKEN_ATTRIBUTE) { return "NMTOKEN"; } 637 else if ( type == Attribute.NMTOKENS_ATTRIBUTE) { return "NMTOKENS"; } 638 else if ( type == Attribute.NOTATION_ATTRIBUTE) { return "NOTATION"; } 639 else if ( type == Attribute.ENUMERATED_ATTRIBUTE) { return "ENUMERATED"; } 640 else { return ""; } 641 642 } 643 644 private void traverseDOM(Element el) { 645 List attrList = el.getAttributes(); 646 AttributesImpl attrs = new AttributesImpl (); 647 for (int i=0; i < attrList.size(); i++) { 648 Attribute attr =(Attribute)attrList.get(i); 649 attrs.addAttribute(attr.getNamespaceURI(), attr.getName(), 650 getQualifiedName(attr), getAttributeType(attr), 651 attr.getValue()); 652 } 653 654 startElement(el.getName(), attrs); 655 656 String text = el.getTextTrim(); 658 if (text != null && text.length() != 0) 659 characters(text); 660 661 List children = el.getChildren(); 662 for (int i=0; i < children.size(); i++) 663 traverseDOM((Element)children.get(i)); 664 665 endElement(); 666 667 } 668 669 670 } 671 672 | Popular Tags |