1 32 33 package com.knowgate.dataxslt; 34 35 import java.lang.ClassNotFoundException ; 36 import java.lang.IllegalAccessException ; 37 38 import java.util.Vector ; 39 import java.util.HashMap ; 40 import java.util.Iterator ; 41 42 import java.io.IOException ; 43 import java.io.FileNotFoundException ; 44 import java.io.FileWriter ; 45 import java.io.FileInputStream ; 46 import java.io.UnsupportedEncodingException ; 47 48 import org.w3c.dom.Element ; 49 import org.w3c.dom.Node ; 50 import org.w3c.dom.NodeList ; 51 52 import dom.DOMDocument; 53 54 import com.knowgate.debug.DebugFile; 55 import com.knowgate.misc.Gadgets; 56 57 63 public class Microsite extends DOMDocument { 64 private Node oMicrositeNode; 65 66 public Microsite() { 67 oMicrositeNode = null; 68 } 69 70 72 public Microsite (String sURI) 73 throws ClassNotFoundException , Exception , IllegalAccessException { 74 80 if (DebugFile.trace) DebugFile.writeln("new Microsite(" + sURI + ")"); 81 82 super.parseURI(sURI); 84 85 Node oTopNode = getRootNode().getFirstChild(); 87 if (oTopNode.getNodeName().equals("xml-stylesheet")) 88 oTopNode = oTopNode.getNextSibling(); 89 90 oMicrositeNode = seekChildByName(oTopNode, "microsite"); 91 92 if (DebugFile.trace) DebugFile.writeln("oMicrositeNode=" + (oMicrositeNode==null ? "null" : "[Element]")); 93 } 94 95 97 public Microsite (String sURI, boolean bValidateXML) 98 throws ClassNotFoundException , Exception , IllegalAccessException { 99 105 super("UTF-8", bValidateXML, false); 106 107 if (DebugFile.trace) 108 DebugFile.writeln("new Microsite(" + sURI + "," + String.valueOf(bValidateXML) + ")"); 109 110 super.setValidation(bValidateXML); 112 super.parseURI(sURI); 113 114 Node oRootNode = getRootNode(); 116 if (null==oRootNode) { 117 throw new NullPointerException ("Cannot find root node for XML document " + sURI); 118 } 119 Node oTopNode = oRootNode.getFirstChild(); 120 if (oTopNode.getNodeName().equals("xml-stylesheet")) 121 oTopNode = oTopNode.getNextSibling(); 122 123 oMicrositeNode = seekChildByName(oTopNode, "microsite"); 124 125 if (DebugFile.trace) DebugFile.writeln("oMicrositeNode=" + (oMicrositeNode==null ? "null" : "[Element]")); 126 } 127 128 130 public String guid() { 131 Node oTopNode; 132 133 if (null==oMicrositeNode) { 134 oTopNode = getRootNode().getFirstChild(); 135 if (oTopNode.getNodeName().equals("xml-stylesheet")) 136 oTopNode = oTopNode.getNextSibling(); 137 if (oTopNode.getNodeName().equals("microsite")) 138 oMicrositeNode = oTopNode; 139 else 140 oMicrositeNode = seekChildByName(oTopNode, "microsite"); 141 } 143 return getAttribute(oMicrositeNode, "guid"); 145 } 147 149 public String name() { 150 Node oTopNode; 151 Element oName; 152 String sName; 153 154 if (DebugFile.trace) { 155 DebugFile.writeln("Begin Microsite.name()"); 156 DebugFile.incIdent(); 157 } 158 159 if (null==oMicrositeNode) { 160 oTopNode = getRootNode().getFirstChild(); 161 162 if (oTopNode.getNodeName().equals("xml-stylesheet")) 163 oTopNode = oTopNode.getNextSibling(); 164 if (oTopNode.getNodeName().equals("microsite")) 165 oMicrositeNode = oTopNode; 166 else 167 oMicrositeNode = seekChildByName(oTopNode, "microsite"); 168 } 170 if (oMicrositeNode!=null) { 171 172 oName = (Element ) seekChildByName(oMicrositeNode, "name"); 174 175 if (oName != null) { 176 sName = oName.getFirstChild().getNodeValue(); 177 } 178 else { 179 if (DebugFile.trace) DebugFile.writeln("ERROR: <name> node not found"); 180 sName = null; 181 } 182 } 183 else { 184 if (DebugFile.trace) DebugFile.writeln("ERROR: <microsite> node not found"); 185 sName = null; 186 } 187 188 if (DebugFile.trace) { 189 DebugFile.decIdent(); 190 DebugFile.writeln("End Microsite.name() : " + sName ); 191 } 192 193 return sName; 194 } 196 198 public Container container(int iIndex) { 199 200 Node oTopNode; 201 Element oContainers; 202 NodeList oNodeList; 203 Container oRetObj; 204 205 if (DebugFile.trace) { 206 DebugFile.writeln("Begin Microsite.container(" + String.valueOf(iIndex) + ")"); 207 DebugFile.incIdent(); 208 } 209 210 if (null==oMicrositeNode) { 212 oTopNode = getRootNode().getFirstChild(); 213 if (oTopNode.getNodeName().equals("xml-stylesheet")) 214 oTopNode = oTopNode.getNextSibling(); 215 if (oTopNode.getNodeName().equals("microsite")) 216 oMicrositeNode = oTopNode; 217 else 218 oMicrositeNode = seekChildByName(oTopNode, "microsite"); 219 } 221 if (oMicrositeNode!=null) { 222 223 oContainers = (Element ) seekChildByName(oMicrositeNode, "containers"); 225 226 if (oContainers!=null) { 227 228 oNodeList = oContainers.getElementsByTagName("container"); 230 231 oRetObj = new Container(oNodeList.item(iIndex)); 232 233 } 234 else { 235 if (DebugFile.trace) DebugFile.writeln("<containers> node not found"); 236 oRetObj = null; 237 } 238 } 239 else { 240 if (DebugFile.trace) DebugFile.writeln("<microsite> node not found"); 241 oRetObj = null; 242 } 243 244 if (DebugFile.trace) { 245 DebugFile.decIdent(); 246 DebugFile.writeln("End Microsite.container() : " + (null==oRetObj ? "null" : "[Container]") ); 247 } 248 249 return oRetObj; 250 } 252 253 255 public Container container(String sGUID) { 256 257 Node oTopNode; 258 Element oContainers, oContainer; 259 NodeList oNodeList; 260 Container oRetObj; 261 262 if (DebugFile.trace) { 263 DebugFile.writeln("Begin Microsite.container(" + sGUID + ")"); 264 DebugFile.incIdent(); 265 } 266 267 if (null==oMicrositeNode) { 269 oTopNode = getRootNode().getFirstChild(); 270 if (oTopNode.getNodeName().equals("xml-stylesheet")) 271 oTopNode = oTopNode.getNextSibling(); 272 if (oTopNode.getNodeName().equals("microsite")) 273 oMicrositeNode = oTopNode; 274 else 275 oMicrositeNode = seekChildByName(oTopNode, "microsite"); 276 } 278 if (oMicrositeNode!=null) { 279 280 oContainers = seekChildByName(oMicrositeNode, "containers"); 281 282 if (oContainers!=null) { 283 284 oContainer = seekChildByAttr(oContainers, "guid", sGUID); 286 287 if (oContainer!=null) { 288 oRetObj = new Container(oContainer); 289 } 290 else { 291 if (DebugFile.trace) DebugFile.writeln("<container guid=\"" + sGUID + "\"> node not found"); 292 oRetObj = null; 293 } 294 } 295 else { 296 if (DebugFile.trace) DebugFile.writeln("<containers> node not found"); 297 oRetObj = null; 298 } 299 } 300 else { 301 if (DebugFile.trace) DebugFile.writeln("<microsite> node not found"); 302 oRetObj = null; 303 } 304 305 if (DebugFile.trace) { 306 DebugFile.decIdent(); 307 DebugFile.writeln("End Microsite.container() : " + (null==oRetObj ? "null" : "[Container]") ); 308 } 309 310 return oRetObj; 311 } 313 315 public Vector containers() { 316 Node oTopNode; 318 Element oContainers; 319 NodeList oNodeList; 320 Vector oLinkVctr; 321 int iContainers; 322 323 if (DebugFile.trace) { 324 DebugFile.writeln("Begin Microsite.containers()"); 325 DebugFile.incIdent(); 326 } 327 328 if (null==oMicrositeNode) { 330 oTopNode = getRootNode().getFirstChild(); 331 if (oTopNode.getNodeName().equals("xml-stylesheet")) 332 oTopNode = oTopNode.getNextSibling(); 333 if (oTopNode.getNodeName().equals("microsite")) 334 oMicrositeNode = oTopNode; 335 else 336 oMicrositeNode = seekChildByName(oTopNode, "microsite"); 337 } 339 if (oMicrositeNode!=null) { 340 341 oContainers = (Element ) seekChildByName(oMicrositeNode, "containers"); 343 344 if (oContainers!=null) { 345 346 oNodeList = oContainers.getElementsByTagName("container"); 348 349 iContainers = oNodeList.getLength(); 351 oLinkVctr = new Vector (iContainers); 352 353 for (int i = 0; i < iContainers; i++) 355 oLinkVctr.add(new Container(oNodeList.item(i))); 356 } 357 else { 358 if (DebugFile.trace) DebugFile.writeln("<containers> node not found"); 359 iContainers = 0; 360 oLinkVctr = null; 361 } 362 } 363 else { 364 if (DebugFile.trace) DebugFile.writeln("<microsite> node not found"); 365 iContainers = 0; 366 oLinkVctr = null; 367 } 368 369 if (DebugFile.trace) { 370 DebugFile.decIdent(); 371 DebugFile.writeln("End Microsite.containers() : " + String.valueOf(iContainers)); 372 } 373 374 return oLinkVctr; 375 } 377 379 public Element seekChildByAttr(Node oParent, String sAttrName, String sAttrValue) { 380 Node oCurrentNode = null; 386 String sCurrentAttr; 387 388 if (DebugFile.trace) { 389 DebugFile.writeln("Begin Microsite.seekChildByAttr(" + (oParent!=null ? "[Node]" : "null") + "," + sAttrName + "," + sAttrValue + ")"); 390 DebugFile.incIdent(); 391 } 392 393 for (oCurrentNode=getFirstElement(oParent); 394 oCurrentNode!=null; 395 oCurrentNode=getNextElement(oCurrentNode)) { 396 sCurrentAttr = getAttribute(oCurrentNode, sAttrName); 397 398 if (sAttrValue.equals(sCurrentAttr)) 399 break; 400 } 402 if (DebugFile.trace) { 403 DebugFile.decIdent(); 404 if (oCurrentNode==null) 405 DebugFile.writeln("End Microsite.seekChildByAttr() : null"); 406 else 407 DebugFile.writeln("End Microsite.seekChildByAttr() : " + oCurrentNode.toString()); 408 } 409 410 if (oCurrentNode!=null) 411 return (Element ) oCurrentNode; 412 else 413 return null; 414 } 416 418 public void createPageSet(String sPath, HashMap oParameters) throws IOException { 419 426 FileWriter oWriter = new FileWriter (sPath); 427 Iterator oKeyIterator; 428 Object oKey; 429 Vector oContainers; 430 int iContainers; 431 432 oWriter.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); 434 oWriter.write("<?xml-stylesheet type=\"text/xsl\"?>\n"); 435 oWriter.write("<?xml-stylesheet type=\"text/xsl\"?>\n"); 436 oWriter.write("<pageset xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"pageset.xsd\" guid=\"" + Gadgets.generateUUID() + "\">\n"); 437 oWriter.write(" <microsite>" + this.guid() + "</microsite>\n"); 438 439 oKeyIterator = oParameters.keySet().iterator(); 440 while (oKeyIterator.hasNext()) { 441 oKey = oKeyIterator.next(); 442 oWriter.write(" <" + oKey.toString() + ">" + oParameters.get(oKey).toString() + "</" + oKey.toString() + ">\n"); 443 } oKeyIterator = null; 445 446 oWriter.write(" <pages>\n"); 447 448 oContainers = this.containers(); 449 iContainers = oContainers.size(); 450 451 for (int p=0; p<iContainers; p++) { 452 oWriter.write(" <page guid=\">" + Gadgets.generateUUID() + "\">\n"); 453 oWriter.write(" <title>Pagina " + String.valueOf(p) + "</title>\n"); 454 oWriter.write(" <container>" + ((Container) oContainers.get(p)).guid() + "</container>\n"); 455 oWriter.write(" <blocks>\n"); 456 oWriter.write(" </blocks>\n"); 457 oWriter.write(" </page>\n"); 458 } 460 oWriter.write(" </pages>\n"); 461 oWriter.write("</pageset>\n"); 462 463 oWriter.close(); 464 oWriter = null; 465 } 467 470 public static String getMicrositeGUID(String sMicrositeURI) throws FileNotFoundException , IOException { 471 String sXML; 472 int iMSiteOpenQuote, iMSiteCloseQuote; 473 byte byXML[] = new byte[1024];; 474 FileInputStream oXMLStream = new FileInputStream (sMicrositeURI); 475 int iReaded = oXMLStream.read(byXML, 0, 1024); 476 oXMLStream.close(); 477 478 sXML = new String (byXML, 0, iReaded); 479 iMSiteOpenQuote = sXML.indexOf("guid")+4; 480 481 for (char b=sXML.charAt(iMSiteOpenQuote); 482 b==' ' || b=='\r' || b=='\n' || b=='\t' || b=='"' || b=='='; 483 b=sXML.charAt(++iMSiteOpenQuote)) ; 484 485 iMSiteCloseQuote = sXML.indexOf("\"", iMSiteOpenQuote); 486 487 return sXML.substring(iMSiteOpenQuote, iMSiteCloseQuote); 488 } 490 492 private static void printUsage() { 493 494 System.out.println(""); 495 System.out.println("Usage:"); 496 System.out.println("com.knowgate.dataxslt.Microsite parse file_path"); 497 } 498 499 501 public static void main(String [] argv) 502 throws IllegalAccessException , ClassNotFoundException , Exception { 503 if (argv.length!=2) 504 printUsage(); 505 else if (!argv[0].equalsIgnoreCase("parse")) 506 printUsage(); 507 else { 508 Microsite oMSite = new Microsite(argv[1], true); 509 } 510 } 512 515 public static final short ClassId = 70; 516 517 }
| Popular Tags
|