1 32 33 package com.knowgate.dataxslt; 34 35 import java.lang.ClassNotFoundException ; 36 import java.lang.IllegalAccessException ; 37 import java.lang.StringBuffer ; 38 39 import java.util.LinkedList ; 40 import java.util.Vector ; 41 import java.util.Properties ; 42 import java.util.HashMap ; 43 44 import java.sql.Statement ; 45 import java.sql.ResultSet ; 46 import java.sql.ResultSetMetaData ; 47 import java.sql.SQLException ; 48 49 import java.io.IOException ; 50 import java.io.FileNotFoundException ; 51 import java.io.UnsupportedEncodingException ; 52 import java.io.InputStream ; 53 import java.io.File ; 54 import java.io.FileReader ; 55 import java.io.FileWriter ; 56 import java.io.FileInputStream ; 57 import java.io.FileOutputStream ; 58 import java.io.ByteArrayOutputStream ; 59 import java.io.StringWriter ; 60 import java.io.UTFDataFormatException ; 61 62 import java.net.URL ; 63 import java.net.MalformedURLException ; 64 65 import javax.activation.DataHandler ; 66 67 import javax.xml.transform.Transformer ; 68 import javax.xml.transform.stream.StreamResult ; 69 import javax.xml.transform.stream.StreamSource ; 70 import javax.xml.transform.TransformerException ; 71 import javax.xml.transform.TransformerConfigurationException ; 72 import javax.xml.transform.SourceLocator ; 73 import javax.xml.transform.OutputKeys ; 74 75 import org.w3c.dom.Element ; 76 import org.w3c.dom.Node ; 77 import org.w3c.dom.NodeList ; 78 import org.w3c.dom.DOMException ; 79 80 import dom.DOMDocument; 81 82 import org.apache.oro.text.regex.*; 83 84 import com.knowgate.jdc.JDCConnection; 85 86 import com.knowgate.dfs.FileSystem; 87 88 import com.knowgate.debug.DebugFile; 89 90 import com.knowgate.misc.Gadgets; 91 92 import com.knowgate.dataobjs.DB; 93 import com.knowgate.dataobjs.DBTable; 94 import com.knowgate.dataobjs.DBSubset; 95 import com.knowgate.dataobjs.DBPersist; 96 97 102 public class PageSet extends DOMDocument { 103 104 106 private Microsite oMSite; private String sURI; private TransformerException oLastXcpt; 111 114 private void initMicrosite(String sMsiteURI, boolean bValidateXML) 115 throws ClassNotFoundException , Exception , IllegalAccessException ,FileNotFoundException { 116 117 if (DebugFile.trace) DebugFile.writeln("PageSet.initMicrosite(" + sMsiteURI + ",schema-validation=" + String.valueOf(bValidateXML) + ")"); 118 119 121 if (sMsiteURI.startsWith("/") || sMsiteURI.startsWith("\\")) { 122 123 File oMFile = new File (sMsiteURI); 124 125 if (!oMFile.exists()) 126 throw new FileNotFoundException (sMsiteURI + " not found"); 127 128 oMFile = null; 129 130 oMSite = MicrositeFactory.getInstance("file://" + sMsiteURI, bValidateXML); 131 } 132 else 133 oMSite = MicrositeFactory.getInstance(sMsiteURI, bValidateXML); 134 } 135 136 144 public PageSet(String sMsiteURI) 145 throws ClassNotFoundException , IllegalAccessException , FileNotFoundException , Exception { 146 147 initMicrosite(sMsiteURI, this.getValidation()); 148 149 sURI = null; 150 } 152 162 public PageSet(String sMsiteURI, boolean bValidateXML) 163 throws ClassNotFoundException , Exception , IllegalAccessException , FileNotFoundException { 164 super("UTF-8", bValidateXML, false); 165 166 if (DebugFile.trace) DebugFile.writeln("new PageSet(" + sMsiteURI + ",schema-validation=" + String.valueOf(bValidateXML) + ")"); 167 168 initMicrosite(sMsiteURI, bValidateXML); 169 sURI = null; 170 } 172 180 public PageSet(String sMsiteURI, String sPageSetURI) 181 throws ClassNotFoundException , Exception , IllegalAccessException , FileNotFoundException { 182 183 if (DebugFile.trace) DebugFile.writeln("new PageSet(" + sMsiteURI + "," + sPageSetURI + ")"); 184 185 File oPFile; 186 187 if (sMsiteURI.startsWith("file://")) 188 initMicrosite(sMsiteURI, this.getValidation()); 189 else 190 initMicrosite("file://" + sMsiteURI, this.getValidation()); 191 192 sURI = sPageSetURI; 193 194 if (sPageSetURI.startsWith("file://")) { 195 oPFile = new File (sPageSetURI.substring(7)); 196 197 if (!oPFile.exists()) 198 throw new FileNotFoundException (sPageSetURI.substring(7) + " not found"); 199 200 if (DebugFile.trace) DebugFile.writeln("parseURI (" + sPageSetURI + ");"); 201 202 parseURI(sPageSetURI); 203 } 204 else { 205 oPFile = new File (sPageSetURI); 206 207 if (!oPFile.exists()) 208 throw new FileNotFoundException (sPageSetURI + " not found"); 209 210 if (DebugFile.trace) DebugFile.writeln("parseURI (file://" + sPageSetURI + ");"); 211 212 parseURI("file://" + sPageSetURI); 213 } 214 216 oPFile = null; 217 218 } 220 229 230 public PageSet(String sMsiteURI, String sPageSetURI, boolean bValidateXML) 231 throws ClassNotFoundException , Exception , IllegalAccessException , FileNotFoundException { 232 233 super("UTF-8", bValidateXML, false); 234 235 if (DebugFile.trace) DebugFile.writeln("new PageSet(" + sMsiteURI + "," + sPageSetURI + ",schema-validation=" + String.valueOf(bValidateXML) + ")"); 236 237 File oPFile; 238 239 if (sMsiteURI.startsWith("file://")) 240 initMicrosite(sMsiteURI, bValidateXML); 241 else 242 initMicrosite("file://" + sMsiteURI, bValidateXML); 243 244 sURI = sPageSetURI; 245 246 if (sPageSetURI.startsWith("file://")) { 247 oPFile = new File (sPageSetURI.substring(7)); 248 249 if (!oPFile.exists()) 250 throw new FileNotFoundException (sPageSetURI.substring(7) + " not found"); 251 252 parseURI(sPageSetURI); 253 } 254 else { 255 oPFile = new File (sPageSetURI); 256 257 if (!oPFile.exists()) 258 throw new FileNotFoundException (sPageSetURI + " not found"); 259 260 parseURI("file://" + sPageSetURI); 261 } 262 } 264 266 269 public String guid() { 270 Node oPageSetNode = getRootNode().getFirstChild(); 271 272 if (oPageSetNode.getNodeName().equals("xml-stylesheet")) oPageSetNode = oPageSetNode.getNextSibling(); 273 274 return getAttribute(oPageSetNode, "guid"); 276 } 278 280 283 public TransformerException lastException() { 284 return oLastXcpt; 285 } 286 287 289 public Microsite microsite() { 290 return oMSite; 291 } 292 293 295 300 301 public String catalog() throws DOMException { 302 303 Node oCatalogNode; 304 String sRetVal; 305 306 if (DebugFile.trace) 307 DebugFile.writeln("Begin PageSet.catalog()"); 308 309 if (DebugFile.trace) DebugFile.incIdent(); 310 311 Node oPageSetNode = getRootNode().getFirstChild(); 313 if (oPageSetNode.getNodeName().equals("xml-stylesheet")) oPageSetNode = oPageSetNode.getNextSibling(); 314 315 oCatalogNode = seekChildByName(oPageSetNode, "catalog"); 317 318 if (oCatalogNode==null) 319 sRetVal = null; 320 else 321 sRetVal = oCatalogNode.getFirstChild().getNodeValue(); 322 323 if (DebugFile.trace) { 324 DebugFile.decIdent(); 325 DebugFile.writeln("End PageSet.catalog() : " + sRetVal); 326 } 327 328 return sRetVal; 329 } 331 333 338 339 public String company() throws DOMException { 340 341 Node oCompanyNode; 342 String sRetVal; 343 344 if (DebugFile.trace) 345 DebugFile.writeln("Begin PageSet.company()"); 346 347 if (DebugFile.trace) DebugFile.incIdent(); 348 349 Node oPageSetNode = getRootNode().getFirstChild(); 351 if (oPageSetNode.getNodeName().equals("xml-stylesheet")) oPageSetNode = oPageSetNode.getNextSibling(); 352 353 oCompanyNode = seekChildByName(oPageSetNode, "company"); 355 356 if (oCompanyNode==null) 357 sRetVal = null; 358 else 359 sRetVal = oCompanyNode.getFirstChild().getNodeValue(); 360 361 if (DebugFile.trace) { 362 DebugFile.decIdent(); 363 DebugFile.writeln("End PageSet.company() : " + sRetVal); 364 } 365 366 return sRetVal; 367 } 369 371 378 379 public Page page(String sPageId) throws DOMException ,NullPointerException { 380 381 Node oPagesNode; 382 Element oContainers; 383 NodeList oNodeList; 384 Page oCurrent; 385 Page oRetVal = null; 386 387 if (DebugFile.trace) 388 DebugFile.writeln("Begin PageSet.page(" + sPageId + ")"); 389 390 if (null==sPageId) 391 throw new NullPointerException ("PageSet.page(), parameter sPageId may not be null"); 392 393 if (DebugFile.trace) DebugFile.incIdent(); 394 395 Node oPageSetNode = getRootNode().getFirstChild(); 397 if (oPageSetNode.getNodeName().equals("xml-stylesheet")) oPageSetNode = oPageSetNode.getNextSibling(); 398 399 oPagesNode = seekChildByName(oPageSetNode, "pages"); 401 402 if (oPagesNode==null) 403 throw new DOMException (DOMException.NOT_FOUND_ERR, "<pages> node not found"); 404 405 oNodeList = ((Element )oPagesNode).getElementsByTagName("page"); 406 407 if (oNodeList.getLength()>0) { 408 409 for (int i=0; i<oNodeList.getLength() && (null==oRetVal); i++) { 411 oCurrent = new Page(oNodeList.item(i), this); 412 if (sPageId.equals(oCurrent.guid())) 413 oRetVal = oCurrent; 414 } } 417 if (DebugFile.trace) { 418 DebugFile.decIdent(); 419 DebugFile.writeln("End PageSet.page() : " + oRetVal); 420 } 421 422 return oRetVal; 423 } 425 427 432 public Vector pages() throws DOMException { 433 Node oPagesNode; 434 Element oContainers; 435 NodeList oNodeList; 436 Vector oLinkVctr; 437 438 if (DebugFile.trace) { 439 DebugFile.writeln("Begin PageSet.pages()"); 440 DebugFile.incIdent(); 441 } 442 443 Node oPageSetNode = getRootNode().getFirstChild(); 445 if (oPageSetNode.getNodeName().equals("xml-stylesheet")) oPageSetNode = oPageSetNode.getNextSibling(); 446 447 oPagesNode = seekChildByName(oPageSetNode, "pages"); 449 450 if (oPagesNode==null) 451 throw new DOMException (DOMException.NOT_FOUND_ERR, "<pages> node not found"); 452 453 oNodeList = ((Element )oPagesNode).getElementsByTagName("page"); 454 455 if (oNodeList.getLength()>0) { 456 oLinkVctr = new Vector (oNodeList.getLength()); 458 459 for (int i=0; i<oNodeList.getLength(); i++) 461 oLinkVctr.add(new Page (oNodeList.item(i), this)); 462 } 463 else 464 oLinkVctr = new Vector (); 465 466 if (DebugFile.trace) { 467 DebugFile.decIdent(); 468 DebugFile.writeln("End PageSet.pages()"); 469 } 470 471 return oLinkVctr; 472 } 474 476 481 public Vector addresses() throws DOMException { 482 Node oPagesNode; 483 Element oContainers; 484 NodeList oNodeList; 485 Vector oLinkVctr; 486 487 if (DebugFile.trace) { 488 DebugFile.writeln("Begin PageSet.pages()"); 489 DebugFile.incIdent(); 490 } 491 492 Node oPageSetNode = getRootNode().getFirstChild(); 494 if (oPageSetNode.getNodeName().equals("xml-stylesheet")) oPageSetNode = oPageSetNode.getNextSibling(); 495 496 oPagesNode = seekChildByName(oPageSetNode, "addresses"); 498 499 if (oPagesNode==null) 500 throw new DOMException (DOMException.NOT_FOUND_ERR, "<addresses> node not found"); 501 502 oNodeList = ((Element )oPagesNode).getElementsByTagName("address"); 503 504 if (oNodeList.getLength()>0) { 505 oLinkVctr = new Vector (oNodeList.getLength()); 507 508 for (int i=0; i<oNodeList.getLength(); i++) 510 oLinkVctr.add(new Page (oNodeList.item(i), this)); 511 } 512 else 513 oLinkVctr = new Vector (); 514 515 if (DebugFile.trace) { 516 DebugFile.decIdent(); 517 DebugFile.writeln("End PageSet.addresses()"); 518 } 519 520 return oLinkVctr; 521 } 523 525 private LinkedList matchChildsByTag(Node oParent, String sPattern) { 526 532 Node oCurrentNode = null; Node oTag = null; NodeList oChilds = oParent.getChildNodes(); int iMaxNodes = oChilds.getLength(); LinkedList oList = new LinkedList (); 538 Pattern oPattern = null; 539 PatternMatcher oMatcher = new Perl5Matcher(); 540 PatternCompiler oCompiler = new Perl5Compiler(); 541 542 try { 543 oPattern = oCompiler.compile(sPattern); 545 } catch(MalformedPatternException e) {} 546 547 for (int iNode=0; iNode<iMaxNodes; iNode++) { 549 oCurrentNode = oChilds.item(iNode); 551 if(Node.ELEMENT_NODE==oCurrentNode.getNodeType()) { 552 553 oTag = seekChildByName(oCurrentNode, "tag"); 555 if (null!=oTag) 556 if (oMatcher.matches(getTextValue((Element )oTag), oPattern)) 558 oList.addLast(oCurrentNode); 559 } } 562 return oList; 563 } 565 566 568 579 public Vector buildSite(String sBasePath, String sOutputPath, Properties oEnvironmentProps, Properties oUserProps) 580 throws IOException , DOMException , TransformerException , TransformerConfigurationException { 581 582 Transformer oTransformer; 583 StreamResult oStreamResult; 584 StreamSource oStreamSrcXML; 585 InputStream oXMLStream = null; 586 String sMedia; 587 Page oCurrentPage; 588 long lElapsed = 0; 589 590 final String sSep = System.getProperty("file.separator"); 591 592 if (DebugFile.trace) { 593 lElapsed = System.currentTimeMillis(); 594 595 DebugFile.writeln("Begin PageSet.BuildSite(" + sBasePath + "," + sOutputPath + "...)"); 596 DebugFile.incIdent(); 597 } 598 599 oLastXcpt = null; 600 601 if (!sBasePath.endsWith(sSep)) sBasePath += sSep; 602 603 Vector vPages = pages(); 604 605 if (DebugFile.trace) 607 DebugFile.writeln("seekChildByName(,[Node], \"containers\")"); 608 609 Node oContainers = oMSite.seekChildByName(oMSite.getRootNode().getFirstChild(), "containers"); 610 611 if (oContainers==null) { 612 if (DebugFile.trace) 613 DebugFile.writeln("ERROR: <containers> node not found."); 614 615 throw new DOMException (DOMException.NOT_FOUND_ERR, "<containers> node not found"); 616 } 617 618 if (DebugFile.trace) 620 DebugFile.writeln("oXMLStream = new FileInputStream(" + sURI + ")"); 621 622 for (int c=0; c<vPages.size(); c++) { 624 oCurrentPage = (Page) vPages.get(c); 625 626 oXMLStream = new FileInputStream (sURI); 627 oStreamSrcXML = new StreamSource (oXMLStream); 628 629 try { 631 if (DebugFile.trace) 632 DebugFile.writeln("oTransformer = StylesheetCache.newTransformer(" + sBasePath + "xslt" + sSep + "templates" + sSep + oMSite.name() + sSep + oCurrentPage.template() + ")"); 633 634 oTransformer = StylesheetCache.newTransformer(sBasePath + "xslt" + sSep + "templates" + sSep + oMSite.name() + sSep + oCurrentPage.template()); 636 637 sMedia = oTransformer.getOutputProperty(OutputKeys.MEDIA_TYPE); 638 if (null==sMedia) 639 sMedia = "html"; 640 else 641 sMedia = sMedia.substring(sMedia.indexOf('/')+1); 642 643 if (DebugFile.trace) 644 DebugFile.writeln("Pages[" + String.valueOf(c) + "].filePath(" + sOutputPath + oCurrentPage.getTitle().replace(' ','_') + "." + sMedia + ")"); 645 646 oCurrentPage.filePath(sOutputPath + oCurrentPage.getTitle().replace(' ','_') + "." + sMedia); 647 648 if (DebugFile.trace) 649 DebugFile.writeln("oStreamResult = new StreamResult(" + oCurrentPage.filePath() + ")"); 650 651 oStreamResult = new StreamResult (oCurrentPage.filePath()); 652 653 StylesheetCache.setParameters(oTransformer, oEnvironmentProps); 655 656 StylesheetCache.setParameters(oTransformer, oUserProps); 658 659 if (DebugFile.trace) 661 DebugFile.writeln("oTransformer.transform(oStreamSrcXML, oStreamResult)"); 662 663 oTransformer.setParameter("param_page", ((Page)(vPages.get(c))).getTitle()); 664 oTransformer.transform(oStreamSrcXML, oStreamResult); 665 } 666 catch (TransformerConfigurationException e) { 667 oLastXcpt = e; 668 if (DebugFile.trace) DebugFile.writeln("ERROR TransformerConfigurationException " + e.getMessageAndLocation()); 669 } 670 catch (TransformerException e) { 671 oLastXcpt = e; 672 if (DebugFile.trace) DebugFile.writeln("ERROR TransformerException " + e.getMessageAndLocation()); 673 } 674 675 oTransformer = null; 676 oStreamResult = null; 677 } 679 oXMLStream.close(); 680 681 if (DebugFile.trace) { 682 DebugFile.writeln("done in " + String.valueOf(System.currentTimeMillis()-lElapsed) + " miliseconds"); 683 684 DebugFile.decIdent(); 685 DebugFile.writeln("End PageSet.buildSite()"); 686 } 687 688 return vPages; 689 } 691 692 694 public Page buildPageForEdit(String sPageGUID, String sBasePath, 695 String sOutputPath, String sCtrlPath, 696 String sMenuPath, String sIntegradorPath, 697 String sSelPageOptions, 698 Properties oEnvironmentProps, Properties oUserProps) 699 700 throws IOException , DOMException , TransformerException , 701 TransformerConfigurationException , MalformedURLException { 702 703 Transformer oTransformer; 704 StreamResult oStreamResult; 705 StreamSource oStreamSrcXML; 706 StringWriter oStrWritter; 707 InputStream oXMLStream = null; 708 String sTransformed; 709 StringBuffer oPostTransform; 710 String sKey; 711 String sMedia; 712 Object sVal; 713 Page oCurrentPage; 714 715 int iCloseHead, iOpenBody, iCloseBody; 716 int iReaded; 717 char CharBuffer[] = new char[8192]; 718 String sCharBuffer; 719 long lElapsed = 0; 720 721 final String sSep = System.getProperty("file.separator"); 722 723 if (DebugFile.trace) { 724 lElapsed = System.currentTimeMillis(); 725 726 DebugFile.writeln("Begin Pageset.buildPageForEdit(" + sBasePath + "," + sOutputPath + "," + sCtrlPath + "," + sMenuPath + ")"); 727 DebugFile.incIdent(); 728 } 729 730 FileSystem oFS = new FileSystem(); 731 732 if (!sBasePath.endsWith(sSep)) sBasePath += sSep; 733 734 String sWebServer = oEnvironmentProps.getProperty("webserver", ""); 735 736 if (DebugFile.trace && sWebServer.length()==0) DebugFile.writeln("WARNING: webserver property not set at EnvironmentProperties"); 737 738 if (!sWebServer.endsWith("/")) sWebServer+="/"; 739 740 741 Node oContainers = oMSite.seekChildByName(oMSite.getRootNode().getFirstChild(), "containers"); 743 744 if (oContainers==null) { 745 if (DebugFile.trace) 746 DebugFile.writeln("ERROR: <containers> node not found."); 747 748 throw new DOMException (DOMException.NOT_FOUND_ERR, "<containers> node not found"); 749 } 750 751 if (DebugFile.trace) 753 DebugFile.writeln("new FileInputStream(" + (sURI.startsWith("file://") ? sURI.substring(7) : sURI) + ")"); 754 755 756 758 oCurrentPage = this.page(sPageGUID); 759 760 oXMLStream = new FileInputStream (sURI.startsWith("file://") ? sURI.substring(7) : sURI); 761 oStreamSrcXML = new StreamSource (oXMLStream); 762 763 oStrWritter = new StringWriter (); 765 oStreamResult = new StreamResult (oStrWritter); 766 767 try { 769 770 oTransformer = StylesheetCache.newTransformer(sBasePath + "xslt" + sSep + "templates" + sSep + oMSite.name() + sSep + oCurrentPage.template()); 772 773 sMedia = oTransformer.getOutputProperty(OutputKeys.MEDIA_TYPE); 774 775 if (DebugFile.trace) DebugFile.writeln(OutputKeys.MEDIA_TYPE + "=" + sMedia); 776 777 if (null==sMedia) 778 sMedia = "html"; 779 else 780 sMedia = sMedia.substring(sMedia.indexOf('/')+1); 781 782 if (null==oCurrentPage.getTitle()) 783 throw new NullPointerException ("Page title is null"); 784 785 if (DebugFile.trace) 786 DebugFile.writeln("Page.filePath(" + sOutputPath + oCurrentPage.getTitle().replace(' ','_') + "." + sMedia + ")"); 787 788 oCurrentPage.filePath(sOutputPath + oCurrentPage.getTitle().replace(' ','_') + "." + sMedia); 789 790 StylesheetCache.setParameters (oTransformer, oEnvironmentProps); 792 793 StylesheetCache.setParameters (oTransformer, oUserProps); 795 796 oTransformer.setParameter ("param_page", oCurrentPage.getTitle()); 798 799 oTransformer.transform (oStreamSrcXML, oStreamResult); 801 802 } 803 catch (TransformerConfigurationException e) { 804 oLastXcpt = e; 805 sMedia = null; 806 807 SourceLocator sl = e.getLocator(); 808 809 if (DebugFile.trace) { 810 if (sl == null) { 811 DebugFile.writeln("ERROR TransformerConfigurationException " + e.getMessage()); 812 } 813 else { 814 DebugFile.writeln("ERROR TransformerConfigurationException " + e.getMessage() + " line=" + String.valueOf(sl.getLineNumber()) + " column=" + String.valueOf(sl.getColumnNumber())); 815 } 816 } 817 } 818 catch (TransformerException e) { 819 oLastXcpt = e; 820 sMedia = null; 821 822 if (DebugFile.trace) DebugFile.writeln("ERROR TransformerException " + e.getMessageAndLocation()); 823 } 824 825 oTransformer = null; 826 oStreamResult = null; 827 828 sTransformed = oStrWritter.toString(); 830 831 if (DebugFile.trace) DebugFile.writeln("transformation length=" + String.valueOf(sTransformed.length())); 832 833 if (sTransformed.length()>0) { 835 iCloseHead = sTransformed.indexOf("</head"); 836 if (iCloseHead<0) iCloseHead = sTransformed.indexOf("</HEAD"); 837 838 iOpenBody = sTransformed.indexOf("<body", iCloseHead); 840 if (iOpenBody<0) iOpenBody = sTransformed.indexOf("<BODY", iCloseHead); 841 842 iCloseBody = sTransformed.indexOf(">", iOpenBody+5); 843 for (char s = sTransformed.charAt(iCloseBody+1); s=='\r' || s=='\n' || s==' ' || s=='\t'; s = sTransformed.charAt(++iCloseBody)) ; 844 845 oPostTransform = new StringBuffer (sTransformed.length()+4096); 847 848 oPostTransform.append(sTransformed.substring(0, iCloseHead)); 850 oPostTransform.append("\n<script language=\"JavaScript\" SRC=\"" + sMenuPath + "\"></script>"); 851 oPostTransform.append("\n<script language=\"JavaScript\" SRC=\"" + sIntegradorPath + "\"></script>\n"); 852 oPostTransform.append(sTransformed.substring(iCloseHead, iCloseHead+7)); 853 oPostTransform.append(sTransformed.substring(iOpenBody, iCloseBody)); 854 855 try { 857 sCharBuffer = oFS.readfilestr(sCtrlPath, "UTF-8"); 858 859 if (DebugFile.trace) DebugFile.writeln(String.valueOf(sCharBuffer.length()) + " characters readed"); 860 } 861 catch (com.enterprisedt.net.ftp.FTPException ftpe) { 862 throw new IOException (ftpe.getMessage()); 863 } 864 865 try { 866 if (DebugFile.trace) DebugFile.writeln("Gadgets.replace(" + sCtrlPath + ",http://demo.hipergate.com/," + sWebServer + ")"); 867 868 Gadgets.replace(sCharBuffer, "http://demo.hipergate.com/", sWebServer); 869 870 } catch (org.apache.oro.text.regex.MalformedPatternException e) { } 871 872 oPostTransform.append("<!--Begin " + sCtrlPath + "-->\n"); 873 874 oPostTransform.append(sCharBuffer); 875 sCharBuffer = null; 876 877 oPostTransform.append("\n<!--End " + sCtrlPath + "-->\n"); 878 879 oPostTransform.append(sTransformed.substring(iCloseBody)); 880 } 881 else { 882 oPostTransform = new StringBuffer ("Page " + oCurrentPage.getTitle() + " could not be rendered."); 883 if (oLastXcpt!=null) oPostTransform.append("<BR>" + oLastXcpt.getMessageAndLocation()); 884 } 885 886 888 if (sSelPageOptions.length()==0) 889 oFS.writefilestr(sOutputPath + oCurrentPage.getTitle().replace(' ','_') + "_." + sMedia, oPostTransform.toString(), "UTF-8"); 890 else 891 try { 892 893 oFS.writefilestr(sOutputPath + oCurrentPage.getTitle().replace(' ','_') + "_." + sMedia, Gadgets.replace(oPostTransform.toString(), ":selPageOptions", sSelPageOptions), "UTF-8"); 894 895 } catch (Exception e) { } 896 897 oPostTransform = null; 899 sTransformed = null; 900 901 oXMLStream.close(); 902 903 if (DebugFile.trace) { 904 DebugFile.writeln("done in " + String.valueOf(System.currentTimeMillis()-lElapsed) + " miliseconds"); 905 906 DebugFile.decIdent(); 907 DebugFile.writeln("End Pageset.buildPageForEdit() : " + oCurrentPage.getTitle()); 908 } 909 910 return oCurrentPage; 911 } 913 915 932 public void buildSiteForEdit(String sBasePath, String sOutputPath, 933 String sCtrlPath, String sMenuPath, 934 String sIntegradorPath, String sSelPageOptions, 935 Properties oEnvironmentProps, Properties oUserProps) 936 937 throws IOException , DOMException , TransformerException , 938 TransformerConfigurationException , MalformedURLException { 939 940 Transformer oTransformer; 941 StreamResult oStreamResult; 942 StreamSource oStreamSrcXML; 943 StringWriter oStrWritter; 944 InputStream oXMLStream = null; 945 String sTransformed; 946 StringBuffer oPostTransform; 947 String sKey; 948 String sMedia; 949 Object sVal; 950 Page oCurrentPage; 951 952 int iCloseHead, iOpenBody, iCloseBody; 953 int iReaded; 954 char CharBuffer[] = new char[8192]; 955 String sCharBuffer; 956 long lElapsed = 0; 957 958 final String sSep = System.getProperty("file.separator"); 959 960 if (DebugFile.trace) { 961 lElapsed = System.currentTimeMillis(); 962 963 DebugFile.writeln("Begin Pageset.buildSiteForEdit(" + sBasePath + "," + sOutputPath + "," + sCtrlPath + "," + sMenuPath + ")"); 964 DebugFile.incIdent(); 965 } 966 967 FileSystem oFS = new FileSystem(); 968 969 Vector vPages = pages(); 970 971 if (!sBasePath.endsWith(sSep)) sBasePath += sSep; 972 973 String sWebServer = oEnvironmentProps.getProperty("webserver", ""); 974 975 if (DebugFile.trace && sWebServer.length()==0) DebugFile.writeln("WARNING: webserver property not set at EnvironmentProperties"); 976 977 if (!sWebServer.endsWith("/")) sWebServer+="/"; 978 979 980 Node oContainers = oMSite.seekChildByName(oMSite.getRootNode().getFirstChild(), "containers"); 982 983 if (oContainers==null) { 984 if (DebugFile.trace) 985 DebugFile.writeln("ERROR: <containers> node not found."); 986 987 throw new DOMException (DOMException.NOT_FOUND_ERR, "<containers> node not found"); 988 } 989 990 if (DebugFile.trace) 992 DebugFile.writeln("new FileInputStream(" + (sURI.startsWith("file://") ? sURI.substring(7) : sURI) + ")"); 993 994 995 for (int c=0; c<vPages.size(); c++) { 997 998 oCurrentPage = (Page) vPages.get(c); 999 1000 oXMLStream = new FileInputStream (sURI.startsWith("file://") ? sURI.substring(7) : sURI); 1001 oStreamSrcXML = new StreamSource (oXMLStream); 1002 1003 oStrWritter = new StringWriter (); 1005 oStreamResult = new StreamResult (oStrWritter); 1006 1007 try { 1009 1010 oTransformer = StylesheetCache.newTransformer(sBasePath + "xslt" + sSep + "templates" + sSep + oMSite.name() + sSep + oCurrentPage.template()); 1012 1013 sMedia = oTransformer.getOutputProperty(OutputKeys.MEDIA_TYPE); 1014 1015 if (DebugFile.trace) DebugFile.writeln(OutputKeys.MEDIA_TYPE + "=" + sMedia); 1016 1017 if (null==sMedia) 1018 sMedia = "html"; 1019 else 1020 sMedia = sMedia.substring(sMedia.indexOf('/')+1); 1021 1022 if (null==oCurrentPage.getTitle()) 1023 throw new NullPointerException ("Page " + String.valueOf(c) + " title is null"); 1024 1025 if (DebugFile.trace) 1026 DebugFile.writeln("Page.filePath(" + sOutputPath + oCurrentPage.getTitle().replace(' ','_') + "." + sMedia + ")"); 1027 1028 oCurrentPage.filePath(sOutputPath + oCurrentPage.getTitle().replace(' ','_') + "." + sMedia); 1029 1030 StylesheetCache.setParameters (oTransformer, oEnvironmentProps); 1032 1033 StylesheetCache.setParameters (oTransformer, oUserProps); 1035 1036 oTransformer.setParameter ("param_page", ((Page)(vPages.get(c))).getTitle()); 1038 1039 oTransformer.transform (oStreamSrcXML, oStreamResult); 1041 1042 } 1043 catch (TransformerConfigurationException e) { 1044 oLastXcpt = e; 1045 sMedia = null; 1046 1047 SourceLocator sl = e.getLocator(); 1048 1049 if (DebugFile.trace) { 1050 if (sl == null) { 1051 DebugFile.writeln("ERROR TransformerConfigurationException " + e.getMessage()); 1052 } 1053 else { 1054 DebugFile.writeln("ERROR TransformerConfigurationException " + e.getMessage() + " line=" + String.valueOf(sl.getLineNumber()) + " column=" + String.valueOf(sl.getColumnNumber())); 1055 } 1056 } 1057 } 1058 catch (TransformerException e) { 1059 oLastXcpt = e; 1060 sMedia = null; 1061 1062 if (DebugFile.trace) DebugFile.writeln("ERROR TransformerException " + e.getMessageAndLocation()); 1063 } 1064 1065 oTransformer = null; 1066 oStreamResult = null; 1067 1068 sTransformed = oStrWritter.toString(); 1070 1071 if (DebugFile.trace) DebugFile.writeln("transformation length=" + String.valueOf(sTransformed.length())); 1072 1073 if (sTransformed.length()>0) { 1075 iCloseHead = sTransformed.indexOf("</head"); 1076 if (iCloseHead<0) iCloseHead = sTransformed.indexOf("</HEAD"); 1077 1078 iOpenBody = sTransformed.indexOf("<body", iCloseHead); 1080 if (iOpenBody<0) iOpenBody = sTransformed.indexOf("<BODY", iCloseHead); 1081 1082 iCloseBody = sTransformed.indexOf(">", iOpenBody+5); 1083 for (char s = sTransformed.charAt(iCloseBody+1); s=='\r' || s=='\n' || s==' ' || s=='\t'; s = sTransformed.charAt(++iCloseBody)) ; 1084 1085 oPostTransform = new StringBuffer (sTransformed.length()+4096); 1087 1088 oPostTransform.append(sTransformed.substring(0, iCloseHead)); 1090 oPostTransform.append("\n<script language=\"JavaScript\" SRC=\"" + sMenuPath + "\"></script>"); 1091 oPostTransform.append("\n<script language=\"JavaScript\" SRC=\"" + sIntegradorPath + "\"></script>\n"); 1092 oPostTransform.append(sTransformed.substring(iCloseHead, iCloseHead+7)); 1093 oPostTransform.append(sTransformed.substring(iOpenBody, iCloseBody)); 1094 1095 try { 1097 sCharBuffer = oFS.readfilestr(sCtrlPath, "UTF-8"); 1098 1099 if (DebugFile.trace) DebugFile.writeln(String.valueOf(sCharBuffer.length()) + " characters readed"); 1100 } 1101 catch (com.enterprisedt.net.ftp.FTPException ftpe) { 1102 throw new IOException (ftpe.getMessage()); 1103 } 1104 1105 try { 1106 if (DebugFile.trace) DebugFile.writeln("Gadgets.replace(" + sCtrlPath + ",http://demo.hipergate.com/," + sWebServer + ")"); 1107 1108 Gadgets.replace(sCharBuffer, "http://demo.hipergate.com/", sWebServer); 1109 } catch (org.apache.oro.text.regex.MalformedPatternException e) { } 1110 1111 oPostTransform.append("<!--Begin " + sCtrlPath + "-->\n"); 1112 1113 oPostTransform.append(sCharBuffer); 1114 sCharBuffer = null; 1115 1116 oPostTransform.append("\n<!--End " + sCtrlPath + "-->\n"); 1117 1118 oPostTransform.append(sTransformed.substring(iCloseBody)); 1119 } 1120 else { 1121 oPostTransform = new StringBuffer ("Page " + ((Page)vPages.get(c)).getTitle() + " could not be rendered."); 1122 if (oLastXcpt!=null) oPostTransform.append("<BR>" + oLastXcpt.getMessageAndLocation()); 1123 } 1124 1125 if (DebugFile.trace) DebugFile.writeln("new FileWriter(" + sOutputPath + oCurrentPage.getTitle().replace(' ','_') + "_." + sMedia + ")"); 1127 1128 1129 if (sSelPageOptions.length()==0) 1130 oFS.writefilestr(sOutputPath + oCurrentPage.getTitle().replace(' ','_') + "_." + sMedia, oPostTransform.toString(), "UTF-8"); 1131 else 1132 try { 1133 oFS.writefilestr(sOutputPath + oCurrentPage.getTitle().replace(' ','_') + "_." + sMedia, Gadgets.replace(oPostTransform.toString(), ":selPageOptions", sSelPageOptions), "UTF-8"); 1134 1135 } catch (Exception e) { } 1136 1137 oPostTransform = null; 1139 sTransformed = null; 1140 } 1142 oXMLStream.close(); 1143 1144 if (DebugFile.trace) { 1145 DebugFile.writeln("done in " + String.valueOf(System.currentTimeMillis()-lElapsed) + " miliseconds"); 1146 1147 DebugFile.decIdent(); 1148 DebugFile.writeln("End Pageset.buildSiteForEdit()"); 1149 } 1150 } 1152 1154 private Page findPage(String sPageGUID) { 1155 Vector oPages = this.pages(); 1156 int iPages = oPages.size(); 1157 Page oPage = null; 1158 1159 for (int p=0; p<iPages && oPage==null; p++) 1160 if (sPageGUID.equals(((Page)oPages.get(p)).guid())) 1161 oPage = (Page)oPages.get(p); 1162 1163 return oPage; 1164 } 1166 1168 1179 public String addBlock(String sFilePath, String sPageGUID, String sBlockXML) 1180 throws IllegalAccessException , IOException , ClassNotFoundException , 1181 NumberFormatException , UTFDataFormatException , Exception { 1182 1183 String sBlockId; 1184 Page oPage = null; 1185 long lElapsed = 0; 1186 1187 if (DebugFile.trace) { 1188 lElapsed = System.currentTimeMillis(); 1189 1190 DebugFile.writeln("Begin Pageset.addBlock(" + sFilePath + "," + sPageGUID + ",\n" + sBlockXML + "\n)"); 1191 DebugFile.incIdent(); 1192 } 1193 1194 sURI = sFilePath; 1195 1196 parseURI(sFilePath); 1197 1198 oPage = findPage(sPageGUID); 1199 1200 sBlockId = oPage.nextBlockId(); 1201 1202 try { 1203 sBlockXML = com.knowgate.misc.Gadgets.replace(sBlockXML, "<block>", "<block id=\"" + sBlockId + "\">"); 1204 } 1205 catch (MalformedPatternException mpe) { } 1206 1207 new XMLDocument(sFilePath).addNodeAndSave("pageset/pages/page[@guid='" + oPage.guid() + "']/blocks/block/", sBlockXML); 1208 1209 if (DebugFile.trace) { 1210 DebugFile.writeln("done in " + String.valueOf(System.currentTimeMillis()-lElapsed) + " miliseconds"); 1211 1212 DebugFile.decIdent(); 1213 DebugFile.writeln("End Pageset.addBlock()"); 1214 } 1215 1216 return sBlockId; 1217 } 1219 1221 public void save(String sFilePath) throws IOException { 1222 FileOutputStream oOutFile = new FileOutputStream (sFilePath, false); 1223 1224 print(oOutFile); 1225 1226 oOutFile.close(); 1227 } 1229 1232 1234 1243 public static void mergeCompanyInfo (JDCConnection oConn, String sFilePath, String sCompanyGUID) 1244 throws SQLException , IOException { 1245 1246 Statement oStmt; 1247 ResultSet oRSet; 1248 1249 if (DebugFile.trace) { 1250 DebugFile.writeln("PageSet.mergeCompanyAddresses(JDCConnection," + sFilePath + "," + sCompanyGUID + ")"); 1251 DebugFile.incIdent(); 1252 } 1253 1254 XMLDocument oXMLDoc = new XMLDocument(sFilePath); 1255 1256 String sCategoryGUID = ""; 1257 1258 if (DebugFile.trace) { 1259 DebugFile.writeln("Connection.executeQuery(SELECT " + DB.gu_category + " FROM " + DB.k_x_company_prods + " WHERE " + DB.gu_company + "='" + sCompanyGUID + "')"); 1260 } 1261 1262 oStmt = oConn.createStatement(); 1263 oRSet = oStmt.executeQuery("SELECT " + DB.gu_category + " FROM " + DB.k_x_company_prods + " WHERE " + DB.gu_company + "='" + sCompanyGUID + "'"); 1264 if (oRSet.next()) { 1265 sCategoryGUID = oRSet.getString(1); 1266 } 1267 oRSet.close(); 1268 oStmt.close(); 1269 1270 oStmt = oConn.createStatement(); 1271 oRSet = oStmt.executeQuery("SELECT * FROM " + DB.k_addresses + " WHERE 1=0"); 1272 ResultSetMetaData oMDat = oRSet.getMetaData(); 1273 StringBuffer oColumnList = new StringBuffer (512); 1274 int iColumnCount = oMDat.getColumnCount(); 1275 1276 for (int c=1; c<=iColumnCount; c++) { 1277 if (c>1) oColumnList.append(','); 1278 oColumnList.append("a." + oMDat.getColumnName(c).toLowerCase()); 1279 } 1281 oRSet.close(); 1282 oStmt.close(); 1283 1284 DBSubset oAddrs = new DBSubset(DB.k_addresses + " a," + DB.k_x_company_addr + " x", 1285 oColumnList.toString(), "a." + DB.gu_address + "=x." + DB.gu_address + " AND x." + DB.gu_company + "=? ORDER BY a." + DB.ix_address, 10); 1286 1287 oAddrs.load(oConn, new Object []{sCompanyGUID}); 1288 1289 String sAddresses = "\n <company>" + sCompanyGUID + "</company>\n <catalog>" + sCategoryGUID + "</catalog>\n <addresses>\n" + oAddrs.toXML(" ", "address") + "\n </addresses>"; 1290 1291 try { oXMLDoc.removeNode("pageset/company"); } 1292 catch (DOMException dome) { 1293 if (dome.code!=DOMException.NOT_FOUND_ERR) throw new DOMException (dome.code, dome.getMessage()); 1294 } 1295 try { oXMLDoc.removeNode("pageset/catalog"); } 1296 catch (DOMException dome) { 1297 if (dome.code!=DOMException.NOT_FOUND_ERR) throw new DOMException (dome.code, dome.getMessage()); 1298 } 1299 try { oXMLDoc.removeNode("pageset/addresses"); } 1300 catch (DOMException dome) { 1301 if (dome.code!=DOMException.NOT_FOUND_ERR) throw new DOMException (dome.code, dome.getMessage()); 1302 } 1303 1304 oXMLDoc.addNodeAndSave("pageset/pages", sAddresses); 1305 1306 if (DebugFile.trace) { 1307 DebugFile.decIdent(); 1308 DebugFile.writeln("PageSet.mergeCompanyAddresses()"); 1309 } 1310 } 1312 1314 1322 public static void removePage(String sFilePath, String sPageGUID) throws IOException { 1323 XMLDocument oXThis = new XMLDocument(sFilePath); 1324 1325 oXThis.removeNodeAndSave("pageset/pages/page[@guid='" + sPageGUID + "']"); 1326 } 1328 1330 1338 public static void removePageByTitle(String sFilePath, String sPageGUIDAttr) throws IOException { 1339 XMLDocument oXThis = new XMLDocument(sFilePath); 1340 1341 oXThis.removeNodeAndSave("pageset/pages/page[guid = '" + sPageGUIDAttr + "']"); 1342 } 1344 1346 1355 public static void removeBlock(String sFilePath, String sPageGUID, String sBlockId) throws IOException { 1356 XMLDocument oXThis = new XMLDocument(sFilePath); 1357 1358 oXThis.removeNodeAndSave("pageset/pages/page[@guid='" + sPageGUID + "']/blocks/block[@id='" + sBlockId + "']"); 1359 } 1361 1363 1371 public static String getMicrositeGUID(String sPageSetURI) 1372 throws FileNotFoundException , IOException { 1373 1374 if (DebugFile.trace) { 1375 DebugFile.writeln("PageSet.getMicrositeGUID(" + sPageSetURI + ")"); 1376 DebugFile.incIdent(); 1377 } 1378 1379 String sXML; 1380 int iMSiteOpenTag, iMSiteCloseTag; 1381 byte byXML[] = new byte[1024]; 1382 FileInputStream oXMLStream = new FileInputStream (sPageSetURI); 1383 int iReaded = oXMLStream.read(byXML, 0, 1024); 1384 oXMLStream.close(); 1385 1386 sXML = new String (byXML, 0, iReaded); 1387 iMSiteOpenTag = sXML.indexOf("<microsite>")+11; 1388 iMSiteCloseTag = sXML.indexOf("</microsite>", iMSiteOpenTag); 1389 1390 if (DebugFile.trace) { 1391 DebugFile.decIdent(); 1392 DebugFile.writeln("PageSet.getMicrositeGUID() : " + sXML.substring(iMSiteOpenTag, iMSiteCloseTag)); 1393 } 1394 1395 return sXML.substring(iMSiteOpenTag, iMSiteCloseTag); 1396 } 1398 1400 private static void printUsage() { 1401 1402 System.out.println(""); 1403 System.out.println("Usage:"); 1404 System.out.println("com.knowgate.dataxslt.PageSet parse file_path"); 1405 } 1406 1407 1409 public static void main(String [] argv) 1410 throws IllegalAccessException , ClassNotFoundException , Exception { 1411 if (argv.length!=2) 1412 printUsage(); 1413 else if (!argv[0].equalsIgnoreCase("parse")) 1414 printUsage(); 1415 else { 1416 PageSet oMSite = new PageSet(argv[1], true); 1417 } 1418 } 1420 1423 public static final short ClassId = 71; 1424 1425}
| Popular Tags
|