KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.ClassNotFoundException JavaDoc;
36 import java.lang.IllegalAccessException JavaDoc;
37
38 import java.util.Vector JavaDoc;
39 import java.util.HashMap JavaDoc;
40 import java.util.Iterator JavaDoc;
41
42 import java.io.IOException JavaDoc;
43 import java.io.FileNotFoundException JavaDoc;
44 import java.io.FileWriter JavaDoc;
45 import java.io.FileInputStream JavaDoc;
46 import java.io.UnsupportedEncodingException JavaDoc;
47
48 import org.w3c.dom.Element JavaDoc;
49 import org.w3c.dom.Node JavaDoc;
50 import org.w3c.dom.NodeList JavaDoc;
51
52 import dom.DOMDocument;
53
54 import com.knowgate.debug.DebugFile;
55 import com.knowgate.misc.Gadgets;
56
57 /**
58  * Microsite DOMDocument.
59  * Metadata for a PageSet.
60  * @author Sergio Montoro Ten
61  * @version 1.0
62  */

63 public class Microsite extends DOMDocument {
64   private Node JavaDoc oMicrositeNode;
65
66   public Microsite() {
67     oMicrositeNode = null;
68   }
69
70   // ----------------------------------------------------------
71

72   public Microsite (String JavaDoc sURI)
73     throws ClassNotFoundException JavaDoc, Exception JavaDoc, IllegalAccessException JavaDoc {
74     // Crea un �rbol DOM en memoria a partir de un archivo XML de definici�n
75
// Par�metros:
76
// sURI -> Ruta absoluta al documento XML de definici�n
77
// este documento debe validar con el schema
78
// microsite.xsd
79

80     if (DebugFile.trace) DebugFile.writeln("new Microsite(" + sURI + ")");
81
82     // Cargar el documento DOM desde una ruta en disco
83
super.parseURI(sURI);
84
85     // Asignar una referencia interna permanente al nodo de nivel superior
86
Node JavaDoc 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   // ----------------------------------------------------------
96

97   public Microsite (String JavaDoc sURI, boolean bValidateXML)
98     throws ClassNotFoundException JavaDoc, Exception JavaDoc, IllegalAccessException JavaDoc {
99     // Crea un �rbol DOM en memoria a partir de un archivo XML de definici�n
100
// Par�metros:
101
// sURI -> Ruta absoluta al documento XML de definici�n
102
// este documento debe validar con el schema
103
// microsite.xsd
104

105     super("UTF-8", bValidateXML, false);
106
107     if (DebugFile.trace)
108       DebugFile.writeln("new Microsite(" + sURI + "," + String.valueOf(bValidateXML) + ")");
109
110     // Cargar el documento DOM desde una ruta en disco
111
super.setValidation(bValidateXML);
112     super.parseURI(sURI);
113
114     // Asignar una referencia interna permanente al nodo de nivel superior
115
Node JavaDoc oRootNode = getRootNode();
116     if (null==oRootNode) {
117       throw new NullPointerException JavaDoc ("Cannot find root node for XML document " + sURI);
118     }
119     Node JavaDoc 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   // ----------------------------------------------------------
129

130   public String JavaDoc guid() {
131     Node JavaDoc 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     } // (oMicrositeNode)
142

143     // Valor del atributo guid del nodo <microsite>
144
return getAttribute(oMicrositeNode, "guid");
145   } // guid()
146

147   // ----------------------------------------------------------
148

149   public String JavaDoc name() {
150     Node JavaDoc oTopNode;
151     Element JavaDoc oName;
152     String JavaDoc 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     } // (oMicrositeNode)
169

170     if (oMicrositeNode!=null) {
171
172       // Buscar el nodo <name>
173
oName = (Element JavaDoc) 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   } // name()
195

196   // ----------------------------------------------------------
197

198   public Container container(int iIndex) {
199
200     Node JavaDoc oTopNode;
201     Element JavaDoc oContainers;
202     NodeList JavaDoc oNodeList;
203     Container oRetObj;
204
205     if (DebugFile.trace) {
206       DebugFile.writeln("Begin Microsite.container(" + String.valueOf(iIndex) + ")");
207       DebugFile.incIdent();
208     }
209
210     // Obtener una referencia al nodo de nivel superior en el documento
211
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     } // (oMicrositeNode)
220

221     if (oMicrositeNode!=null) {
222
223       // Buscar el nodo <containers>
224
oContainers = (Element JavaDoc) seekChildByName(oMicrositeNode, "containers");
225
226       if (oContainers!=null) {
227
228         // Obtener una lista de nodos cuyo nombre sea <container>
229
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   } // container
251

252
253   // ----------------------------------------------------------
254

255   public Container container(String JavaDoc sGUID) {
256
257     Node JavaDoc oTopNode;
258     Element JavaDoc oContainers, oContainer;
259     NodeList JavaDoc oNodeList;
260     Container oRetObj;
261
262     if (DebugFile.trace) {
263       DebugFile.writeln("Begin Microsite.container(" + sGUID + ")");
264       DebugFile.incIdent();
265     }
266
267     // Obtener una referencia al nodo de nivel superior en el documento
268
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     } // (oMicrositeNode)
277

278     if (oMicrositeNode!=null) {
279
280       oContainers = seekChildByName(oMicrositeNode, "containers");
281
282       if (oContainers!=null) {
283
284         // Buscar el nodo <container> con el guid especificado
285
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   } // container
312

313   // ----------------------------------------------------------
314

315   public Vector JavaDoc containers() {
316     // Devuelve un vector con los contenedores de este Microsite
317
Node JavaDoc oTopNode;
318     Element JavaDoc oContainers;
319     NodeList JavaDoc oNodeList;
320     Vector JavaDoc oLinkVctr;
321     int iContainers;
322
323     if (DebugFile.trace) {
324       DebugFile.writeln("Begin Microsite.containers()");
325       DebugFile.incIdent();
326     }
327
328     // Obtener una referencia al nodo de nivel superior en el documento
329
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     } // (oMicrositeNode)
338

339     if (oMicrositeNode!=null) {
340
341       // Buscar el nodo <containers>
342
oContainers = (Element JavaDoc) seekChildByName(oMicrositeNode, "containers");
343
344       if (oContainers!=null) {
345
346         // Obtener una lista de nodos cuyo nombre sea <container>
347
oNodeList = oContainers.getElementsByTagName("container");
348
349         // Crear el vector
350
iContainers = oNodeList.getLength();
351         oLinkVctr = new Vector JavaDoc(iContainers);
352
353         // Convertir los nodos DOM en objetos de tipo Container
354
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   } // containers()
376

377   // ----------------------------------------------------------
378

379   public Element JavaDoc seekChildByAttr(Node JavaDoc oParent, String JavaDoc sAttrName, String JavaDoc sAttrValue) {
380     // Busca un nodo hijo del nivel inmediatamente inferior cuyo atributo tenga un valor determinado
381
// Parametros:
382
// oParent -> Nodo Padre
383
// sAttrName -> Nombre del atributo a examinar
384
// sAttrValue -> Valor del atributo buscado
385
Node JavaDoc oCurrentNode = null;
386     String JavaDoc 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     } // next(iNode)
401

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 JavaDoc) oCurrentNode;
412     else
413       return null;
414   } // seekChildByAttr()
415

416   // ----------------------------------------------------------
417

418   public void createPageSet(String JavaDoc sPath, HashMap JavaDoc oParameters) throws IOException JavaDoc {
419     // Crear un nuevo documento PageSet en un archivo
420
// a partir de la definici�n estructural de este Microsite
421
// Par�metros:
422
// sPath -> Ruta al archivo de salida
423
// oParameters -> Par�metros adicionales de creaci�n
424
// (t�picamente: font, color, etc)
425

426     FileWriter JavaDoc oWriter = new FileWriter JavaDoc(sPath);
427     Iterator JavaDoc oKeyIterator;
428     Object JavaDoc oKey;
429     Vector JavaDoc oContainers;
430     int iContainers;
431
432     // Escribir a cap�n los nodos del PageSet
433
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     } // wend()
444
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     } // next(p)
459

460     oWriter.write(" </pages>\n");
461     oWriter.write("</pageset>\n");
462
463     oWriter.close();
464     oWriter = null;
465   } // createPageSet()
466

467   // ***************************************************************************
468
// Static methods
469

470   public static String JavaDoc getMicrositeGUID(String JavaDoc sMicrositeURI) throws FileNotFoundException JavaDoc, IOException JavaDoc {
471     String JavaDoc sXML;
472     int iMSiteOpenQuote, iMSiteCloseQuote;
473     byte byXML[] = new byte[1024];;
474     FileInputStream JavaDoc oXMLStream = new FileInputStream JavaDoc(sMicrositeURI);
475     int iReaded = oXMLStream.read(byXML, 0, 1024);
476     oXMLStream.close();
477
478     sXML = new String JavaDoc(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   } // getMicrositeGUID();
489

490   // ----------------------------------------------------------
491

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   // ---------------------------------------------------------
500

501   public static void main(String JavaDoc[] argv)
502     throws IllegalAccessException JavaDoc, ClassNotFoundException JavaDoc, Exception JavaDoc {
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   } // main
511

512   // ***************************************************************************
513
// Static variables
514

515   public static final short ClassId = 70;
516
517 } // Microsite
Free Books   Free Magazines  
Popular Tags