1 32 33 package com.knowgate.dataxslt; 34 35 import java.util.Vector ; 36 import java.util.SortedMap ; 37 import java.util.TreeMap ; 38 import java.util.Iterator ; 39 40 import com.knowgate.debug.DebugFile; 41 import com.knowgate.misc.Gadgets; 42 43 import org.w3c.dom.Element ; 44 import org.w3c.dom.Node ; 45 import org.w3c.dom.NodeList ; 46 import org.w3c.dom.DOMException ; 47 48 import dom.DOMSubDocument; 49 50 56 57 public class Page extends DOMSubDocument { 58 private String sPhysicalFile; 59 private PageSet oOwnerPageSet; 60 61 63 67 public Page (Node oRefNode, PageSet oPagSet) { 68 super(oRefNode); 69 70 sPhysicalFile = null; 71 oOwnerPageSet = oPagSet; 72 } 73 74 76 79 public PageSet getPaseSet() { 80 return oOwnerPageSet; 81 } 82 83 85 88 public Container getContainer() { 89 return oOwnerPageSet.microsite().container(container()); 90 } 91 92 94 99 public String template() throws DOMException { 100 Microsite oMSite = oOwnerPageSet.microsite(); 101 102 Node oTopNode = oMSite.getRootNode().getFirstChild(); 103 104 if (oTopNode.getNodeName().equalsIgnoreCase("xml-stylesheet")) 105 oTopNode = oTopNode.getNextSibling(); 106 107 Node oContainers = oMSite.seekChildByName(oTopNode, "containers"); 108 109 Node oContainer = (Node ) oMSite.seekChildByAttr(oContainers, "guid", this.container()); 110 111 if (oContainer==null) { 112 if (DebugFile.trace) 113 DebugFile.writeln("ERROR: guid attribute for container " + this.container() + " not found."); 114 115 throw new DOMException (DOMException.NOT_FOUND_ERR, "guid attribute for container " + this.container() + " not found"); 116 } 118 Element oTemplate = oMSite.seekChildByName(oContainer, "template"); 119 120 if (oTemplate==null) { 121 if (DebugFile.trace) 122 DebugFile.writeln("ERROR: <template> node for page " + this.getTitle() + " not found."); 123 124 throw new DOMException (DOMException.NOT_FOUND_ERR, "<template> node for page " + this.getTitle() + " not found"); 125 } 126 127 return oMSite.getTextValue(oTemplate); 128 } 130 132 137 public String filePath() { 138 return sPhysicalFile; 139 } 140 141 143 149 public void filePath(String sPath) { 150 sPhysicalFile = sPath; 151 } 152 153 155 159 public String guid() { 160 Node oItem = oNode.getAttributes().getNamedItem("guid"); 161 162 if (null==oItem) 163 return null; 164 else 165 return oItem.getNodeValue(); 166 } 168 170 174 public String container() { 175 return getElement("container"); 176 } 178 180 183 public String getTitle() { 184 return getElement("title"); 185 } 187 189 193 public void setTitle(String sTitle) { 194 Node oCurrentNode = null; 195 196 for (oCurrentNode=oNode.getFirstChild(); oCurrentNode!=null; oCurrentNode=oCurrentNode.getNextSibling()) 197 if (Node.ELEMENT_NODE==oCurrentNode.getNodeType()) 198 if (oCurrentNode.getNodeName().equals("title")) break; 199 200 oCurrentNode.setNodeValue(sTitle); 201 } 203 205 212 public Vector blocks() 213 throws DOMException { 214 215 if (DebugFile.trace) { 216 DebugFile.writeln("Begin Page.blocks()"); 217 DebugFile.incIdent(); 218 } 219 220 String sPaddedID; 221 Node oBlksNode = null; 222 NodeList oNodeList = null; 223 int iNodeListLen = 0; 224 Vector oLinkVctr = null; 225 SortedMap oSortedMap = new TreeMap (); 226 227 if (DebugFile.trace) { 228 if (null==oNode.getFirstChild()) 229 DebugFile.writeln("Node.getFirstChild() returned null"); 230 } 231 232 for (oBlksNode=oNode.getFirstChild(); oBlksNode!=null; oBlksNode=oBlksNode.getNextSibling()) 233 if (Node.ELEMENT_NODE==oBlksNode.getNodeType()) 234 if (oBlksNode.getNodeName().equals("blocks")) break; 235 236 if (DebugFile.trace) 237 if (null==oBlksNode) 238 DebugFile.writeln("ERROR: blocks node not found"); 239 240 if (null==oBlksNode) 241 throw new DOMException (DOMException.NOT_FOUND_ERR, "<blocks> node not found"); 242 243 oNodeList = ((Element ) oBlksNode).getElementsByTagName("block"); 244 iNodeListLen = oNodeList.getLength(); 245 246 if (DebugFile.trace) 247 DebugFile.writeln(String.valueOf(iNodeListLen) + " blocks found."); 248 249 oLinkVctr = new Vector (iNodeListLen); 250 251 for (int i=0; i<iNodeListLen; i++) { 252 253 if (DebugFile.trace) { 254 if (null==oNodeList.item(i).getAttributes().getNamedItem("id")) 255 DebugFile.writeln("ERROR: Block " + String.valueOf(i) + " does not have the required id attribute."); 256 else 257 if (null==oNodeList.item(i).getAttributes().getNamedItem("id").getNodeValue()) 258 DebugFile.writeln("ERROR: Block " + String.valueOf(i) + " id attribute is null."); 259 else if (oNodeList.item(i).getAttributes().getNamedItem("id").getNodeValue().length()==0) 260 DebugFile.writeln("ERROR: Block " + String.valueOf(i) + " id attribute is empty."); 261 } 262 263 sPaddedID = "-" + Gadgets.leftPad(oNodeList.item(i).getAttributes().getNamedItem("id").getNodeValue(), '0', 3); 264 265 if (DebugFile.trace) 266 DebugFile.writeln("padded id = " + sPaddedID); 267 268 if (DebugFile.trace) { 269 if (((Element )oNodeList.item(i)).getElementsByTagName("metablock").getLength()==0) 270 DebugFile.writeln("ERROR: No MetaBlocks found"); 271 else 272 if (null==((Element )oNodeList.item(i)).getElementsByTagName("metablock").item(0).getFirstChild()) 273 DebugFile.writeln("ERROR: MetaBlock for Block " + String.valueOf(i) + " does not have the requiered id attribute"); 274 else 275 if (((Element )oNodeList.item(i)).getElementsByTagName("metablock").item(0).getFirstChild().getNodeValue().length()==0) 276 DebugFile.writeln("ERROR: MetaBlock for Block " + String.valueOf(i) + " id attribute is empty."); 277 else 278 DebugFile.writeln("SortedMap.put(" + ((Element )oNodeList.item(i)).getElementsByTagName("metablock").item(0).getFirstChild().getNodeValue() + sPaddedID + ", " + oNodeList.item(i).toString()); 279 } 280 281 oSortedMap.put(((Element )oNodeList.item(i)).getElementsByTagName("metablock").item(0).getFirstChild().getNodeValue() + sPaddedID, oNodeList.item(i)); 282 } 284 Iterator oIterator = oSortedMap.keySet().iterator(); 285 while (oIterator.hasNext()) { 286 Node oAux = (Node ) oSortedMap.get(oIterator.next()); 287 oLinkVctr.add(new Block(oAux)); 288 if (DebugFile.trace) 289 DebugFile.writeln("Inserted " + ((Element )oAux).getElementsByTagName("metablock").item(0).getFirstChild().getNodeValue() + (new Integer (oAux.getAttributes().getNamedItem("id").getNodeValue())).intValue()); 290 } 292 oNodeList = null; 293 oBlksNode = null; 294 oSortedMap = null; 295 296 if (DebugFile.trace) { 297 DebugFile.decIdent(); 298 DebugFile.writeln("End Page.blocks()"); 299 } 300 301 return oLinkVctr; 302 } 304 306 private boolean eqnull (String s1, String s2) { 307 if (DebugFile.trace) DebugFile.writeln("eqnull(" + s1 + "," + s2 + ")"); 308 309 if (s1==null) 310 return true; 311 else 312 return s1.equals(s2); 313 } 315 324 public Vector blocks(String sMetaBlockId, String sTag, String sZone) 325 throws DOMException { 326 327 if (DebugFile.trace) { 328 DebugFile.writeln("Begin Page.blocks(" + sMetaBlockId + "," + sTag + "," + sZone + ")"); 329 DebugFile.incIdent(); 330 } 331 332 Block oBlk; 333 Node oAux; 334 Node oBlksNode = null; 335 NodeList oNodeList = null; 336 int iNodeListLen = 0; 337 Vector oLinkVctr = null; 338 339 if (DebugFile.trace) { 340 if (null==oNode.getFirstChild()) 341 DebugFile.writeln("Node.getFirstChild() returned null"); 342 } 343 344 for (oBlksNode=oNode.getFirstChild(); oBlksNode!=null; oBlksNode=oBlksNode.getNextSibling()) 345 if (Node.ELEMENT_NODE==oBlksNode.getNodeType()) 346 if (oBlksNode.getNodeName().equals("blocks")) break; 347 348 if (DebugFile.trace) 349 if (null==oBlksNode) 350 DebugFile.writeln("ERROR: blocks node not found"); 351 352 if (null==oBlksNode) 353 throw new DOMException (DOMException.NOT_FOUND_ERR, "<blocks> node not found"); 354 355 oNodeList = ((Element ) oBlksNode).getElementsByTagName("block"); 356 iNodeListLen = oNodeList.getLength(); 357 358 if (DebugFile.trace) 359 DebugFile.writeln(String.valueOf(iNodeListLen) + " total blocks found."); 360 361 oLinkVctr = new Vector (); 362 363 for (int i=0; i<iNodeListLen; i++) { 364 oAux = oNodeList.item(i); 365 366 if (DebugFile.trace) { 367 DebugFile.writeln("scanning " + oAux); 368 369 if (null==oAux.getAttributes().getNamedItem("id")) 370 DebugFile.writeln("ERROR: Block " + String.valueOf(i) + " does not have the required id attribute."); 371 else 372 if (null==oAux.getAttributes().getNamedItem("id").getNodeValue()) 373 DebugFile.writeln("ERROR: Block " + String.valueOf(i) + " id attribute is null."); 374 else if (oAux.getAttributes().getNamedItem("id").getNodeValue().length()==0) 375 DebugFile.writeln("ERROR: Block " + String.valueOf(i) + " id attribute is empty."); 376 377 if (((Element )oAux).getElementsByTagName("metablock").getLength()==0) 378 DebugFile.writeln("ERROR: No MetaBlocks found"); 379 else 380 if (null==((Element )oAux).getElementsByTagName("metablock").item(0).getFirstChild()) 381 DebugFile.writeln("ERROR: MetaBlock for Block " + String.valueOf(i) + " does not have the requiered id attribute"); 382 else 383 if (((Element )oAux).getElementsByTagName("metablock").item(0).getFirstChild().getNodeValue().length()==0) 384 DebugFile.writeln("ERROR: MetaBlock for Block " + String.valueOf(i) + " id attribute is empty."); 385 386 DebugFile.writeln("new Block(" + oAux + ")"); 387 388 } 390 oBlk = new Block(oAux); 391 392 if (eqnull(sMetaBlockId, oBlk.metablock()) && eqnull(sTag, oBlk.tag()) && eqnull(sZone, oBlk.zone())) { 393 394 if (DebugFile.trace) DebugFile.writeln("Vector.add(" + oBlk.id() + ")"); 395 396 oLinkVctr.add(oBlk); 397 } 398 } 400 401 oNodeList = null; 402 oBlksNode = null; 403 404 if (DebugFile.trace) { 405 DebugFile.decIdent(); 406 DebugFile.writeln("End Page.blocks()"); 407 } 408 409 return oLinkVctr; 410 } 412 414 419 public int[] blockIds() throws NumberFormatException { 420 Node oBlksNode = null; 421 NodeList oNodeList = null; 422 int iNodeListLen = 0; 423 424 for (oBlksNode=oNode.getFirstChild(); oBlksNode!=null; oBlksNode=oBlksNode.getNextSibling()) 425 if (Node.ELEMENT_NODE==oBlksNode.getNodeType()) 426 if (oBlksNode.getNodeName().equals("blocks")) break; 427 428 oNodeList = ((Element ) oBlksNode).getElementsByTagName("block"); 429 iNodeListLen = oNodeList.getLength(); 430 431 int[] aIds = new int[iNodeListLen]; 432 433 for (int i=0; i<iNodeListLen; i++) 434 aIds[i] = Integer.parseInt(oNodeList.item(i).getAttributes().getNamedItem("id").getNodeValue()); 435 436 return aIds; 437 } 439 441 446 public String nextBlockId() throws NumberFormatException { 447 448 if (DebugFile.trace) { 449 DebugFile.writeln("Begin Page.nextBlockId()"); 450 DebugFile.incIdent(); 451 } 452 453 int iBlk = 0; 454 int iMax = 0; 455 String sNext; 456 Node oBlksNode = null; 457 NodeList oNodeList = null; 458 int iNodeListLen = 0; 459 460 for (oBlksNode=oNode.getFirstChild(); oBlksNode!=null; oBlksNode=oBlksNode.getNextSibling()) 461 if (Node.ELEMENT_NODE==oBlksNode.getNodeType()) 462 if (oBlksNode.getNodeName().equals("blocks")) break; 463 464 if (DebugFile.trace) 465 if (null==oBlksNode) 466 DebugFile.writeln("ERROR: blocks node not found"); 467 468 oNodeList = ((Element ) oBlksNode).getElementsByTagName("block"); 469 iNodeListLen = oNodeList.getLength(); 470 471 int[] aBlocks = new int[iNodeListLen]; 472 473 for (int i=0; i<iNodeListLen; i++) 474 aBlocks[i] = Integer.parseInt(oNodeList.item(i).getAttributes().getNamedItem("id").getNodeValue()); 475 476 for (int b=0; b<iNodeListLen; b++) { 477 iBlk = aBlocks[b]; 478 if (iBlk>iMax) iMax=iBlk; 479 } 481 sNext = String.valueOf(iMax+1); 483 if (sNext.length()==1) 484 sNext = "00" + sNext; 485 else if (sNext.length()==2) 486 sNext = "0" + sNext; 487 488 if (DebugFile.trace) { 489 DebugFile.decIdent(); 490 DebugFile.writeln("End Page.nextBlockId() : " + sNext); 491 } 492 493 return sNext; 494 } 496 498 504 public void permute (int[] aNewBlcksPerm) 505 throws ArrayIndexOutOfBoundsException { 506 507 if (DebugFile.trace) { 508 DebugFile.writeln("Begin Page.permute()"); 509 DebugFile.incIdent(); 510 } 511 512 int iCurrent; 513 int[] aOldBlcksPerm = blockIds(); 514 515 if (aOldBlcksPerm.length!=aNewBlcksPerm.length) 517 throw new ArrayIndexOutOfBoundsException ("Length of Blocks array does not match Block count for already loaded Document"); 518 519 Vector vOldPerm = blocks(); 520 int iBlckCount = vOldPerm.size(); 521 522 if (iBlckCount>0) { 523 Vector vNewPerm = new Vector (iBlckCount); 524 525 for (int blck=0; blck<iBlckCount; blck++) { 527 iCurrent = aNewBlcksPerm[blck]; 528 529 for (int oldpos=0; oldpos<iBlckCount; oldpos++) { 530 531 if (aOldBlcksPerm[oldpos]==iCurrent) { 532 vNewPerm.add (new Block(((Block)vOldPerm.get(oldpos)).getNode().cloneNode(true))); 533 break; 534 } 535 } } 538 if (DebugFile.trace) { 540 if (null==oNode.getFirstChild()) 541 DebugFile.writeln("Node.getFirstChild() returned null"); 542 } 543 544 Node oBlksNode = null; 545 546 for (oBlksNode=oNode.getFirstChild(); oBlksNode!=null; oBlksNode=oBlksNode.getNextSibling()) 547 if (Node.ELEMENT_NODE==oBlksNode.getNodeType()) 548 if (oBlksNode.getNodeName().equals("blocks")) break; 549 550 if (DebugFile.trace) 551 if (null==oBlksNode) 552 DebugFile.writeln("ERROR: blocks node not found"); 553 554 for (int chld=0; chld<iBlckCount; chld++) 556 oBlksNode.removeChild (((Block)vOldPerm.get(chld)).getNode()); 557 558 Block oClon; 560 for (int chld=0; chld<iBlckCount; chld++) { 561 oClon = ( (Block) vNewPerm.get(chld)); 562 563 oClon.id(Gadgets.leftPad(String.valueOf(chld+1), '0', 3)); 566 567 oBlksNode.appendChild( oClon.getNode()); 568 } } 571 if (DebugFile.trace) { 572 DebugFile.decIdent(); 573 DebugFile.writeln("End Page.permute()"); 574 } 575 } } | Popular Tags |