001: /*
002: Copyright (C) 2003 Know Gate S.L. All rights reserved.
003: C/Oña, 107 1º2 28050 Madrid (Spain)
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions
007: are met:
008:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011:
012: 2. The end-user documentation included with the redistribution,
013: if any, must include the following acknowledgment:
014: "This product includes software parts from hipergate
015: (http://www.hipergate.org/)."
016: Alternately, this acknowledgment may appear in the software itself,
017: if and wherever such third-party acknowledgments normally appear.
018:
019: 3. The name hipergate must not be used to endorse or promote products
020: derived from this software without prior written permission.
021: Products derived from this software may not be called hipergate,
022: nor may hipergate appear in their name, without prior written
023: permission.
024:
025: This library is distributed in the hope that it will be useful,
026: but WITHOUT ANY WARRANTY; without even the implied warranty of
027: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
028:
029: You should have received a copy of hipergate License with this code;
030: if not, visit http://www.hipergate.org or mail to info@hipergate.org
031: */
032:
033: package com.knowgate.dataxslt;
034:
035: import java.lang.ClassNotFoundException;
036: import java.lang.IllegalAccessException;
037:
038: import java.util.Vector;
039: import java.util.HashMap;
040: import java.util.Iterator;
041:
042: import java.io.IOException;
043: import java.io.FileNotFoundException;
044: import java.io.FileWriter;
045: import java.io.FileInputStream;
046: import java.io.UnsupportedEncodingException;
047:
048: import org.w3c.dom.Element;
049: import org.w3c.dom.Node;
050: import org.w3c.dom.NodeList;
051:
052: import dom.DOMDocument;
053:
054: import com.knowgate.debug.DebugFile;
055: import com.knowgate.misc.Gadgets;
056:
057: /**
058: * Microsite DOMDocument.
059: * Metadata for a PageSet.
060: * @author Sergio Montoro Ten
061: * @version 1.0
062: */
063: public class Microsite extends DOMDocument {
064: private Node oMicrositeNode;
065:
066: public Microsite() {
067: oMicrositeNode = null;
068: }
069:
070: // ----------------------------------------------------------
071:
072: public Microsite(String sURI) throws ClassNotFoundException,
073: Exception, IllegalAccessException {
074: // Crea un árbol DOM en memoria a partir de un archivo XML de definición
075: // Parámetros:
076: // sURI -> Ruta absoluta al documento XML de definición
077: // este documento debe validar con el schema
078: // microsite.xsd
079:
080: if (DebugFile.trace)
081: DebugFile.writeln("new Microsite(" + sURI + ")");
082:
083: // Cargar el documento DOM desde una ruta en disco
084: super .parseURI(sURI);
085:
086: // Asignar una referencia interna permanente al nodo de nivel superior
087: Node oTopNode = getRootNode().getFirstChild();
088: if (oTopNode.getNodeName().equals("xml-stylesheet"))
089: oTopNode = oTopNode.getNextSibling();
090:
091: oMicrositeNode = seekChildByName(oTopNode, "microsite");
092:
093: if (DebugFile.trace)
094: DebugFile.writeln("oMicrositeNode="
095: + (oMicrositeNode == null ? "null" : "[Element]"));
096: }
097:
098: // ----------------------------------------------------------
099:
100: public Microsite(String sURI, boolean bValidateXML)
101: throws ClassNotFoundException, Exception,
102: IllegalAccessException {
103: // Crea un árbol DOM en memoria a partir de un archivo XML de definición
104: // Parámetros:
105: // sURI -> Ruta absoluta al documento XML de definición
106: // este documento debe validar con el schema
107: // microsite.xsd
108:
109: super ("UTF-8", bValidateXML, false);
110:
111: if (DebugFile.trace)
112: DebugFile.writeln("new Microsite(" + sURI + ","
113: + String.valueOf(bValidateXML) + ")");
114:
115: // Cargar el documento DOM desde una ruta en disco
116: super .setValidation(bValidateXML);
117: super .parseURI(sURI);
118:
119: // Asignar una referencia interna permanente al nodo de nivel superior
120: Node oRootNode = getRootNode();
121: if (null == oRootNode) {
122: throw new NullPointerException(
123: "Cannot find root node for XML document " + sURI);
124: }
125: Node oTopNode = oRootNode.getFirstChild();
126: if (oTopNode.getNodeName().equals("xml-stylesheet"))
127: oTopNode = oTopNode.getNextSibling();
128:
129: oMicrositeNode = seekChildByName(oTopNode, "microsite");
130:
131: if (DebugFile.trace)
132: DebugFile.writeln("oMicrositeNode="
133: + (oMicrositeNode == null ? "null" : "[Element]"));
134: }
135:
136: // ----------------------------------------------------------
137:
138: public String guid() {
139: Node oTopNode;
140:
141: if (null == oMicrositeNode) {
142: oTopNode = getRootNode().getFirstChild();
143: if (oTopNode.getNodeName().equals("xml-stylesheet"))
144: oTopNode = oTopNode.getNextSibling();
145: if (oTopNode.getNodeName().equals("microsite"))
146: oMicrositeNode = oTopNode;
147: else
148: oMicrositeNode = seekChildByName(oTopNode, "microsite");
149: } // (oMicrositeNode)
150:
151: // Valor del atributo guid del nodo <microsite>
152: return getAttribute(oMicrositeNode, "guid");
153: } // guid()
154:
155: // ----------------------------------------------------------
156:
157: public String name() {
158: Node oTopNode;
159: Element oName;
160: String sName;
161:
162: if (DebugFile.trace) {
163: DebugFile.writeln("Begin Microsite.name()");
164: DebugFile.incIdent();
165: }
166:
167: if (null == oMicrositeNode) {
168: oTopNode = getRootNode().getFirstChild();
169:
170: if (oTopNode.getNodeName().equals("xml-stylesheet"))
171: oTopNode = oTopNode.getNextSibling();
172: if (oTopNode.getNodeName().equals("microsite"))
173: oMicrositeNode = oTopNode;
174: else
175: oMicrositeNode = seekChildByName(oTopNode, "microsite");
176: } // (oMicrositeNode)
177:
178: if (oMicrositeNode != null) {
179:
180: // Buscar el nodo <name>
181: oName = (Element) seekChildByName(oMicrositeNode, "name");
182:
183: if (oName != null) {
184: sName = oName.getFirstChild().getNodeValue();
185: } else {
186: if (DebugFile.trace)
187: DebugFile.writeln("ERROR: <name> node not found");
188: sName = null;
189: }
190: } else {
191: if (DebugFile.trace)
192: DebugFile.writeln("ERROR: <microsite> node not found");
193: sName = null;
194: }
195:
196: if (DebugFile.trace) {
197: DebugFile.decIdent();
198: DebugFile.writeln("End Microsite.name() : " + sName);
199: }
200:
201: return sName;
202: } // name()
203:
204: // ----------------------------------------------------------
205:
206: public Container container(int iIndex) {
207:
208: Node oTopNode;
209: Element oContainers;
210: NodeList oNodeList;
211: Container oRetObj;
212:
213: if (DebugFile.trace) {
214: DebugFile.writeln("Begin Microsite.container("
215: + String.valueOf(iIndex) + ")");
216: DebugFile.incIdent();
217: }
218:
219: // Obtener una referencia al nodo de nivel superior en el documento
220: if (null == oMicrositeNode) {
221: oTopNode = getRootNode().getFirstChild();
222: if (oTopNode.getNodeName().equals("xml-stylesheet"))
223: oTopNode = oTopNode.getNextSibling();
224: if (oTopNode.getNodeName().equals("microsite"))
225: oMicrositeNode = oTopNode;
226: else
227: oMicrositeNode = seekChildByName(oTopNode, "microsite");
228: } // (oMicrositeNode)
229:
230: if (oMicrositeNode != null) {
231:
232: // Buscar el nodo <containers>
233: oContainers = (Element) seekChildByName(oMicrositeNode,
234: "containers");
235:
236: if (oContainers != null) {
237:
238: // Obtener una lista de nodos cuyo nombre sea <container>
239: oNodeList = oContainers
240: .getElementsByTagName("container");
241:
242: oRetObj = new Container(oNodeList.item(iIndex));
243:
244: } else {
245: if (DebugFile.trace)
246: DebugFile.writeln("<containers> node not found");
247: oRetObj = null;
248: }
249: } else {
250: if (DebugFile.trace)
251: DebugFile.writeln("<microsite> node not found");
252: oRetObj = null;
253: }
254:
255: if (DebugFile.trace) {
256: DebugFile.decIdent();
257: DebugFile.writeln("End Microsite.container() : "
258: + (null == oRetObj ? "null" : "[Container]"));
259: }
260:
261: return oRetObj;
262: } // container
263:
264: // ----------------------------------------------------------
265:
266: public Container container(String sGUID) {
267:
268: Node oTopNode;
269: Element oContainers, oContainer;
270: NodeList oNodeList;
271: Container oRetObj;
272:
273: if (DebugFile.trace) {
274: DebugFile.writeln("Begin Microsite.container(" + sGUID
275: + ")");
276: DebugFile.incIdent();
277: }
278:
279: // Obtener una referencia al nodo de nivel superior en el documento
280: if (null == oMicrositeNode) {
281: oTopNode = getRootNode().getFirstChild();
282: if (oTopNode.getNodeName().equals("xml-stylesheet"))
283: oTopNode = oTopNode.getNextSibling();
284: if (oTopNode.getNodeName().equals("microsite"))
285: oMicrositeNode = oTopNode;
286: else
287: oMicrositeNode = seekChildByName(oTopNode, "microsite");
288: } // (oMicrositeNode)
289:
290: if (oMicrositeNode != null) {
291:
292: oContainers = seekChildByName(oMicrositeNode, "containers");
293:
294: if (oContainers != null) {
295:
296: // Buscar el nodo <container> con el guid especificado
297: oContainer = seekChildByAttr(oContainers, "guid", sGUID);
298:
299: if (oContainer != null) {
300: oRetObj = new Container(oContainer);
301: } else {
302: if (DebugFile.trace)
303: DebugFile.writeln("<container guid=\"" + sGUID
304: + "\"> node not found");
305: oRetObj = null;
306: }
307: } else {
308: if (DebugFile.trace)
309: DebugFile.writeln("<containers> node not found");
310: oRetObj = null;
311: }
312: } else {
313: if (DebugFile.trace)
314: DebugFile.writeln("<microsite> node not found");
315: oRetObj = null;
316: }
317:
318: if (DebugFile.trace) {
319: DebugFile.decIdent();
320: DebugFile.writeln("End Microsite.container() : "
321: + (null == oRetObj ? "null" : "[Container]"));
322: }
323:
324: return oRetObj;
325: } // container
326:
327: // ----------------------------------------------------------
328:
329: public Vector containers() {
330: // Devuelve un vector con los contenedores de este Microsite
331: Node oTopNode;
332: Element oContainers;
333: NodeList oNodeList;
334: Vector oLinkVctr;
335: int iContainers;
336:
337: if (DebugFile.trace) {
338: DebugFile.writeln("Begin Microsite.containers()");
339: DebugFile.incIdent();
340: }
341:
342: // Obtener una referencia al nodo de nivel superior en el documento
343: if (null == oMicrositeNode) {
344: oTopNode = getRootNode().getFirstChild();
345: if (oTopNode.getNodeName().equals("xml-stylesheet"))
346: oTopNode = oTopNode.getNextSibling();
347: if (oTopNode.getNodeName().equals("microsite"))
348: oMicrositeNode = oTopNode;
349: else
350: oMicrositeNode = seekChildByName(oTopNode, "microsite");
351: } // (oMicrositeNode)
352:
353: if (oMicrositeNode != null) {
354:
355: // Buscar el nodo <containers>
356: oContainers = (Element) seekChildByName(oMicrositeNode,
357: "containers");
358:
359: if (oContainers != null) {
360:
361: // Obtener una lista de nodos cuyo nombre sea <container>
362: oNodeList = oContainers
363: .getElementsByTagName("container");
364:
365: // Crear el vector
366: iContainers = oNodeList.getLength();
367: oLinkVctr = new Vector(iContainers);
368:
369: // Convertir los nodos DOM en objetos de tipo Container
370: for (int i = 0; i < iContainers; i++)
371: oLinkVctr.add(new Container(oNodeList.item(i)));
372: } else {
373: if (DebugFile.trace)
374: DebugFile.writeln("<containers> node not found");
375: iContainers = 0;
376: oLinkVctr = null;
377: }
378: } else {
379: if (DebugFile.trace)
380: DebugFile.writeln("<microsite> node not found");
381: iContainers = 0;
382: oLinkVctr = null;
383: }
384:
385: if (DebugFile.trace) {
386: DebugFile.decIdent();
387: DebugFile.writeln("End Microsite.containers() : "
388: + String.valueOf(iContainers));
389: }
390:
391: return oLinkVctr;
392: } // containers()
393:
394: // ----------------------------------------------------------
395:
396: public Element seekChildByAttr(Node oParent, String sAttrName,
397: String sAttrValue) {
398: // Busca un nodo hijo del nivel inmediatamente inferior cuyo atributo tenga un valor determinado
399: // Parametros:
400: // oParent -> Nodo Padre
401: // sAttrName -> Nombre del atributo a examinar
402: // sAttrValue -> Valor del atributo buscado
403: Node oCurrentNode = null;
404: String sCurrentAttr;
405:
406: if (DebugFile.trace) {
407: DebugFile.writeln("Begin Microsite.seekChildByAttr("
408: + (oParent != null ? "[Node]" : "null") + ","
409: + sAttrName + "," + sAttrValue + ")");
410: DebugFile.incIdent();
411: }
412:
413: for (oCurrentNode = getFirstElement(oParent); oCurrentNode != null; oCurrentNode = getNextElement(oCurrentNode)) {
414: sCurrentAttr = getAttribute(oCurrentNode, sAttrName);
415:
416: if (sAttrValue.equals(sCurrentAttr))
417: break;
418: } // next(iNode)
419:
420: if (DebugFile.trace) {
421: DebugFile.decIdent();
422: if (oCurrentNode == null)
423: DebugFile
424: .writeln("End Microsite.seekChildByAttr() : null");
425: else
426: DebugFile.writeln("End Microsite.seekChildByAttr() : "
427: + oCurrentNode.toString());
428: }
429:
430: if (oCurrentNode != null)
431: return (Element) oCurrentNode;
432: else
433: return null;
434: } // seekChildByAttr()
435:
436: // ----------------------------------------------------------
437:
438: public void createPageSet(String sPath, HashMap oParameters)
439: throws IOException {
440: // Crear un nuevo documento PageSet en un archivo
441: // a partir de la definición estructural de este Microsite
442: // Parámetros:
443: // sPath -> Ruta al archivo de salida
444: // oParameters -> Parámetros adicionales de creación
445: // (típicamente: font, color, etc)
446:
447: FileWriter oWriter = new FileWriter(sPath);
448: Iterator oKeyIterator;
449: Object oKey;
450: Vector oContainers;
451: int iContainers;
452:
453: // Escribir a capón los nodos del PageSet
454: oWriter.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
455: oWriter.write("<?xml-stylesheet type=\"text/xsl\"?>\n");
456: oWriter.write("<?xml-stylesheet type=\"text/xsl\"?>\n");
457: oWriter
458: .write("<pageset xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"pageset.xsd\" guid=\""
459: + Gadgets.generateUUID() + "\">\n");
460: oWriter.write(" <microsite>" + this .guid() + "</microsite>\n");
461:
462: oKeyIterator = oParameters.keySet().iterator();
463: while (oKeyIterator.hasNext()) {
464: oKey = oKeyIterator.next();
465: oWriter.write(" <" + oKey.toString() + ">"
466: + oParameters.get(oKey).toString() + "</"
467: + oKey.toString() + ">\n");
468: } // wend()
469: oKeyIterator = null;
470:
471: oWriter.write(" <pages>\n");
472:
473: oContainers = this .containers();
474: iContainers = oContainers.size();
475:
476: for (int p = 0; p < iContainers; p++) {
477: oWriter.write(" <page guid=\">" + Gadgets.generateUUID()
478: + "\">\n");
479: oWriter.write(" <title>Pagina " + String.valueOf(p)
480: + "</title>\n");
481: oWriter.write(" <container>"
482: + ((Container) oContainers.get(p)).guid()
483: + "</container>\n");
484: oWriter.write(" <blocks>\n");
485: oWriter.write(" </blocks>\n");
486: oWriter.write(" </page>\n");
487: } // next(p)
488:
489: oWriter.write(" </pages>\n");
490: oWriter.write("</pageset>\n");
491:
492: oWriter.close();
493: oWriter = null;
494: } // createPageSet()
495:
496: // ***************************************************************************
497: // Static methods
498:
499: public static String getMicrositeGUID(String sMicrositeURI)
500: throws FileNotFoundException, IOException {
501: String sXML;
502: int iMSiteOpenQuote, iMSiteCloseQuote;
503: byte byXML[] = new byte[1024];
504: ;
505: FileInputStream oXMLStream = new FileInputStream(sMicrositeURI);
506: int iReaded = oXMLStream.read(byXML, 0, 1024);
507: oXMLStream.close();
508:
509: sXML = new String(byXML, 0, iReaded);
510: iMSiteOpenQuote = sXML.indexOf("guid") + 4;
511:
512: for (char b = sXML.charAt(iMSiteOpenQuote); b == ' '
513: || b == '\r' || b == '\n' || b == '\t' || b == '"'
514: || b == '='; b = sXML.charAt(++iMSiteOpenQuote))
515: ;
516:
517: iMSiteCloseQuote = sXML.indexOf("\"", iMSiteOpenQuote);
518:
519: return sXML.substring(iMSiteOpenQuote, iMSiteCloseQuote);
520: } // getMicrositeGUID();
521:
522: // ----------------------------------------------------------
523:
524: private static void printUsage() {
525:
526: System.out.println("");
527: System.out.println("Usage:");
528: System.out
529: .println("com.knowgate.dataxslt.Microsite parse file_path");
530: }
531:
532: // ---------------------------------------------------------
533:
534: public static void main(String[] argv)
535: throws IllegalAccessException, ClassNotFoundException,
536: Exception {
537: if (argv.length != 2)
538: printUsage();
539: else if (!argv[0].equalsIgnoreCase("parse"))
540: printUsage();
541: else {
542: Microsite oMSite = new Microsite(argv[1], true);
543: }
544: } // main
545:
546: // ***************************************************************************
547: // Static variables
548:
549: public static final short ClassId = 70;
550:
551: } // Microsite
|