KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > dataxslt > Page


1 /*
2   Copyright (C) 2003 Know Gate S.L. All rights reserved.
3                       C/Oņa, 107 1š2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.dataxslt;
34
35 import java.util.Vector JavaDoc;
36 import java.util.SortedMap JavaDoc;
37 import java.util.TreeMap JavaDoc;
38 import java.util.Iterator JavaDoc;
39
40 import com.knowgate.debug.DebugFile;
41 import com.knowgate.misc.Gadgets;
42
43 import org.w3c.dom.Element JavaDoc;
44 import org.w3c.dom.Node JavaDoc;
45 import org.w3c.dom.NodeList JavaDoc;
46 import org.w3c.dom.DOMException JavaDoc;
47
48 import dom.DOMSubDocument;
49
50 /**
51  * <p>PageSet Page</p>
52  * <p>This class represents a &lt;page&gt;&lt;/page&gt; section of a PageSet XML definition file.
53  * @author Sergio Montoro Ten
54  * @version 1.1
55  */

56
57 public class Page extends DOMSubDocument {
58   private String JavaDoc sPhysicalFile;
59   private PageSet oOwnerPageSet;
60
61   // ----------------------------------------------------------
62

63   /**
64    * @param oRefNode DOMDocument Node holding &lt;page&gt; element.
65    * @param oPagSet Reference to PageSet object that contains this page
66    */

67   public Page (Node JavaDoc oRefNode, PageSet oPagSet) {
68     super(oRefNode);
69
70     sPhysicalFile = null;
71     oOwnerPageSet = oPagSet;
72   }
73
74   // ----------------------------------------------------------
75

76   /**
77    * Reference to PageSet object that contains this Page
78    */

79   public PageSet getPaseSet() {
80     return oOwnerPageSet;
81   }
82
83   // ----------------------------------------------------------
84

85   /**
86    * Reference to Container object that describes this Page structure
87    */

88   public Container getContainer() {
89     return oOwnerPageSet.microsite().container(container());
90   }
91
92   // ----------------------------------------------------------
93

94   /**
95    * Get XSL transformer stylesheet name for this Page
96    * @return &lt;template&gt; value for Container of this Page
97    * @throws DOMException
98    */

99   public String JavaDoc template() throws DOMException JavaDoc {
100     Microsite oMSite = oOwnerPageSet.microsite();
101
102     Node JavaDoc oTopNode = oMSite.getRootNode().getFirstChild();
103
104     if (oTopNode.getNodeName().equalsIgnoreCase("xml-stylesheet"))
105       oTopNode = oTopNode.getNextSibling();
106
107     Node JavaDoc oContainers = oMSite.seekChildByName(oTopNode, "containers");
108
109     Node JavaDoc oContainer = (Node JavaDoc) 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 JavaDoc(DOMException.NOT_FOUND_ERR, "guid attribute for container " + this.container() + " not found");
116     } // fi
117

118     Element JavaDoc 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 JavaDoc(DOMException.NOT_FOUND_ERR, "<template> node for page " + this.getTitle() + " not found");
125     }
126
127     return oMSite.getTextValue(oTemplate);
128   } // template
129

130   // ----------------------------------------------------------
131

132   /**
133    * @return Path to final document generated after XSL transformation of Page.
134    * This property is not part of the Page XML file but it is set at runtime and
135    * stored appart.
136    */

137   public String JavaDoc filePath() {
138     return sPhysicalFile;
139   }
140
141   // ----------------------------------------------------------
142

143   /**
144    * Set path to final document generated after XSL transformation of Page.
145    * This property is not part of the Page XML file but it is set at runtime and
146    * stored appart.
147    * @param sPath Path to generated XHTML (or other) final file.
148    */

149   public void filePath(String JavaDoc sPath) {
150     sPhysicalFile = sPath;
151   }
152
153   // ----------------------------------------------------------
154

155   /**
156    * Value of attribute guid for this Page at PageSet XML file
157    * @return GUID of this Page
158    */

159   public String JavaDoc guid() {
160     Node JavaDoc oItem = oNode.getAttributes().getNamedItem("guid");
161
162     if (null==oItem)
163       return null;
164     else
165       return oItem.getNodeValue();
166   } // guid()
167

168   // ----------------------------------------------------------
169

170   /**
171    * GUID of Container that describes this Page structure
172    * @return String representing the Container GUID
173    */

174   public String JavaDoc container() {
175     return getElement("container");
176   } // container
177

178   // ----------------------------------------------------------
179

180   /**
181    * @return &lt;title&gt;element contents.
182    */

183   public String JavaDoc getTitle() {
184     return getElement("title");
185   } // getTitle
186

187   // ----------------------------------------------------------
188

189   /**
190    * Set &lt;title&gt;element contents.
191    * @param sTitle
192    */

193   public void setTitle(String JavaDoc sTitle) {
194     Node JavaDoc 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   } // setTitle
202

203   // ----------------------------------------------------------
204

205   /**
206    * Get Page blocks.
207    * @return Vector containing objects of class Block for this Page.<br>
208    * Returned blocks are always sorted by their metablock id attribute and their
209    * block id attribute. The metablock id and the block id are concatenated
210    * @throws DOMException If &lt;blocks&gt; node is not found
211    */

212   public Vector JavaDoc blocks()
213     throws DOMException JavaDoc {
214
215     if (DebugFile.trace) {
216          DebugFile.writeln("Begin Page.blocks()");
217          DebugFile.incIdent();
218        }
219
220     String JavaDoc sPaddedID;
221     Node JavaDoc oBlksNode = null;
222     NodeList JavaDoc oNodeList = null;
223     int iNodeListLen = 0;
224     Vector JavaDoc oLinkVctr = null;
225     SortedMap JavaDoc oSortedMap = new TreeMap JavaDoc();
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 JavaDoc(DOMException.NOT_FOUND_ERR, "<blocks> node not found");
242
243     oNodeList = ((Element JavaDoc) 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 JavaDoc(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 JavaDoc)oNodeList.item(i)).getElementsByTagName("metablock").getLength()==0)
270           DebugFile.writeln("ERROR: No MetaBlocks found");
271         else
272           if (null==((Element JavaDoc)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 JavaDoc)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 JavaDoc)oNodeList.item(i)).getElementsByTagName("metablock").item(0).getFirstChild().getNodeValue() + sPaddedID + ", " + oNodeList.item(i).toString());
279       }
280
281       oSortedMap.put(((Element JavaDoc)oNodeList.item(i)).getElementsByTagName("metablock").item(0).getFirstChild().getNodeValue() + sPaddedID, oNodeList.item(i));
282     } // next (i)
283

284     Iterator JavaDoc oIterator = oSortedMap.keySet().iterator();
285     while (oIterator.hasNext()) {
286       Node JavaDoc oAux = (Node JavaDoc) oSortedMap.get(oIterator.next());
287       oLinkVctr.add(new Block(oAux));
288       if (DebugFile.trace)
289         DebugFile.writeln("Inserted " + ((Element JavaDoc)oAux).getElementsByTagName("metablock").item(0).getFirstChild().getNodeValue() + (new Integer JavaDoc(oAux.getAttributes().getNamedItem("id").getNodeValue())).intValue());
290     } // wend
291

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   } // blocks()
303

304   // ----------------------------------------------------------
305

306   private boolean eqnull (String JavaDoc s1, String JavaDoc 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   } // eqnull
314

315   /**
316    * Get Page blocks matching a given criteria.
317    * @param sMetaBlockId Identifier of metablock to match
318    * @param sTag
319    * @param sZone
320    * @return Vector containing objects of class Block for this Page.<br>
321    * Blocks are returned in source order. Independently of theit id attribute value.
322    * @throws DOMException If &lt;blocks&gt; node is not found
323    */

324   public Vector JavaDoc blocks(String JavaDoc sMetaBlockId, String JavaDoc sTag, String JavaDoc sZone)
325     throws DOMException JavaDoc {
326
327     if (DebugFile.trace) {
328          DebugFile.writeln("Begin Page.blocks(" + sMetaBlockId + "," + sTag + "," + sZone + ")");
329          DebugFile.incIdent();
330        }
331
332     Block oBlk;
333     Node JavaDoc oAux;
334     Node JavaDoc oBlksNode = null;
335     NodeList JavaDoc oNodeList = null;
336     int iNodeListLen = 0;
337     Vector JavaDoc 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 JavaDoc(DOMException.NOT_FOUND_ERR, "<blocks> node not found");
354
355     oNodeList = ((Element JavaDoc) 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 JavaDoc();
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 JavaDoc)oAux).getElementsByTagName("metablock").getLength()==0)
378           DebugFile.writeln("ERROR: No MetaBlocks found");
379         else
380           if (null==((Element JavaDoc)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 JavaDoc)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       } // fi (DebugFile.trace)
389

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     } // next (i)
399

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   } // blocks(...)
411

412   // ----------------------------------------------------------
413

414   /**
415    * <p>Get a list of Block identifiers in source order<p>
416    * @return Array of Block Identifiers in the same order as thay appear in source document.
417    * @throws NumberFormatException If any Block Id. is not an integer
418    */

419   public int[] blockIds() throws NumberFormatException JavaDoc {
420     Node JavaDoc oBlksNode = null;
421     NodeList JavaDoc 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 JavaDoc) 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   } // blockIds
438

439   // ----------------------------------------------------------
440

441   /**
442    * Get next free block integer identifier.
443    * @return Left padded next free block identifier (3 characters)
444    * @throws NumberFormatException If any of the previous block identifiers is not an integer.
445    */

446   public String JavaDoc nextBlockId() throws NumberFormatException JavaDoc {
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 JavaDoc sNext;
456     Node JavaDoc oBlksNode = null;
457     NodeList JavaDoc 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 JavaDoc) 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     } // next (b)
480

481     // left pad with zeros
482
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   } // nextBlockId
495

496   // ----------------------------------------------------------
497

498   /**
499    * <p>Permute Blocks</p>
500    * This method is used for reordering blocks.
501    * @param aNewBlcksPerm New order for blocks.
502    * @throws ArrayIndexOutOfBoundsException
503    */

504   public void permute (int[] aNewBlcksPerm)
505     throws ArrayIndexOutOfBoundsException JavaDoc {
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     // Check that existing and new block permutations have same count of blocks
516
if (aOldBlcksPerm.length!=aNewBlcksPerm.length)
517       throw new ArrayIndexOutOfBoundsException JavaDoc("Length of Blocks array does not match Block count for already loaded Document");
518
519     Vector JavaDoc vOldPerm = blocks();
520     int iBlckCount = vOldPerm.size();
521
522     if (iBlckCount>0) {
523       Vector JavaDoc vNewPerm = new Vector JavaDoc(iBlckCount);
524
525       // Create a Vector with Blocks cloned from the existing ones but in the new order
526
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         } // next (oldpos)
536
} // next (blck)
537

538       // Search for <blocks> Node
539
if (DebugFile.trace) {
540          if (null==oNode.getFirstChild())
541            DebugFile.writeln("Node.getFirstChild() returned null");
542        }
543
544        Node JavaDoc 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        // Remove all old Blocks
555
for (int chld=0; chld<iBlckCount; chld++)
556          oBlksNode.removeChild (((Block)vOldPerm.get(chld)).getNode());
557
558        // Re-insert previously cloned Blocks
559
Block oClon;
560        for (int chld=0; chld<iBlckCount; chld++) {
561          oClon = ( (Block) vNewPerm.get(chld));
562
563          // Assign next block identifier for mantining consistency between
564
// source order and id attributes
565
oClon.id(Gadgets.leftPad(String.valueOf(chld+1), '0', 3));
566
567          oBlksNode.appendChild( oClon.getNode());
568        } // next
569
} // fi (iBlckCount>0)
570

571     if (DebugFile.trace) {
572       DebugFile.decIdent();
573       DebugFile.writeln("End Page.permute()");
574     }
575   } // permute
576
}
Popular Tags