DebugFile.writeln("Begin Pageset.buildPageForEdit(" + sBasePath + "," + sOutputPath + "," + sCtrlPath + "," + sMenuPath + ")");
DebugFile.incIdent();
}
FileSystem oFS = new FileSystem();
if (!sBasePath.endsWith(sSep)) sBasePath += sSep;
String sWebServer = oEnvironmentProps.getProperty("webserver", "");
if (DebugFile.trace && sWebServer.length()==0) DebugFile.writeln("WARNING: webserver property not set at EnvironmentProperties");
if (!sWebServer.endsWith("/")) sWebServer+="/";
// Posicionarse en el nodo de contenedores
Node oContainers = oMSite.seekChildByName(oMSite.getRootNode().getFirstChild(), "containers");
if (oContainers==null) {
if (DebugFile.trace)
DebugFile.writeln("ERROR: <containers> node not found.");
throw new DOMException(DOMException.NOT_FOUND_ERR, "<containers> node not found");
}
// Cagar el stream de datos XML una sola vez
if (DebugFile.trace)
DebugFile.writeln("new FileInputStream(" + (sURI.startsWith("file://") ? sURI.substring(7) : sURI) + ")");
// Para cada contenedor (página) realizar la transformación XSLT
oCurrentPage = this.page(sPageGUID);
oXMLFile = new File (sURI.startsWith("file://") ? sURI.substring(7) : sURI);
if (!oXMLFile.exists()) {
if (DebugFile.trace) DebugFile.decIdent();
throw new FileNotFoundException("PageSet.buildPageForEdit() File not found "+sURI);
}
oXMLStream = new FileInputStream(oXMLFile);
oStreamSrcXML = new StreamSource(oXMLStream);
// Asignar cada stream de salida a su stream temporal
oStrWritter = new StringWriter();
oStreamResult = new StreamResult(oStrWritter);
// Transformacion XSLT
try {
// Obtener la hoja de estilo desde el cache
sXSLFile = sBasePath + "xslt" + sSep + "templates" + sSep + oMSite.name() + sSep + oCurrentPage.template();
oXSLFile = new File (sXSLFile);
if (!oXSLFile.exists()) {
if (DebugFile.trace) DebugFile.decIdent();
throw new FileNotFoundException("PageSet.buildPageForEdit() File not found "+sXSLFile+" maybe there is a mismatch between the microsite name and the directory name where it is placed, or between the template name and the actual .xsl file name");
}
oTransformer = StylesheetCache.newTransformer(sXSLFile);
sMedia = oTransformer.getOutputProperty(OutputKeys.MEDIA_TYPE);
if (DebugFile.trace) DebugFile.writeln(OutputKeys.MEDIA_TYPE + "=" + sMedia);
if (null==sMedia)
sMedia = "html";
else
sMedia = sMedia.substring(sMedia.indexOf('/')+1);
if (null==oCurrentPage.getTitle())
throw new NullPointerException("Page title is null");
if (DebugFile.trace)
DebugFile.writeln("Page.filePath(" + sOutputPath + oCurrentPage.getTitle().replace(' ','_') + "." + sMedia + ")");
oCurrentPage.filePath(sOutputPath + oCurrentPage.getTitle().replace(' ','_') + "." + sMedia);
// Set environment parameters for stylesheet
StylesheetCache.setParameters (oTransformer, oEnvironmentProps);
// Set user defined parameters for stylesheet
StylesheetCache.setParameters (oTransformer, oUserProps);
// Paso el title de la pagina como parametro
oTransformer.setParameter ("param_page", oCurrentPage.getTitle());
// Realizar la transformación
oTransformer.transform (oStreamSrcXML, oStreamResult);
}
catch (TransformerConfigurationException e) {
oLastXcpt = e;
sMedia = null;
SourceLocator sl = e.getLocator();
if (DebugFile.trace) {
if (sl == null) {
DebugFile.writeln("ERROR TransformerConfigurationException " + e.getMessage());
}
else {
DebugFile.writeln("ERROR TransformerConfigurationException " + e.getMessage() + " line=" + String.valueOf(sl.getLineNumber()) + " column=" + String.valueOf(sl.getColumnNumber()));
}
}
}
catch (TransformerException e) {
oLastXcpt = e;
sMedia = null;
if (DebugFile.trace) DebugFile.writeln("ERROR TransformerException " + e.getMessageAndLocation());
}
oTransformer = null;
oStreamResult = null;
// Asignar un String con el fuente XML transformado
sTransformed = oStrWritter.toString();
if (DebugFile.trace) DebugFile.writeln("transformation length=" + String.valueOf(sTransformed.length()));
// Buscar el fin de tag </head>
if (sTransformed.length()>0) {
iCloseHead = sTransformed.indexOf("</head");
if (iCloseHead<0) iCloseHead = sTransformed.indexOf("</HEAD");
if (iCloseHead<0) {
if (DebugFile.trace) {
DebugFile.writeln("Stylesheet lacks </head> tag");
DebugFile.decIdent();
}
throw new TransformerException("Stylesheet lacks </head> tag");
} // fi
// Buscar el inicio de tag <body>
iOpenBody = sTransformed.indexOf("<body", iCloseHead);
if (iOpenBody<0) iOpenBody = sTransformed.indexOf("<BODY", iCloseHead);
if (iOpenBody<0) {
if (DebugFile.trace) {
DebugFile.writeln("Stylesheet lacks <body> tag");
DebugFile.decIdent();
}
throw new TransformerException("Stylesheet lacks <body> tag");
} // fi
iCloseBody = sTransformed.indexOf(">", iOpenBody+5);
for (char s = sTransformed.charAt(iCloseBody+1); s=='\r' || s=='\n' || s==' ' || s=='\t'; s = sTransformed.charAt(++iCloseBody)) ;
// Crear un buffer intermedio para mayor velocidad de concatenado
oPostTransform = new StringBuffer(sTransformed.length()+4096);
// Incrustar las llamadas al Integrador en el lugar apropiado del fuente
oPostTransform.append(sTransformed.substring(0, iCloseHead));
oPostTransform.append("\n<script language=\"JavaScript\" type=\"text/javascript\" src=\"" + sMenuPath + "\"></script>");
oPostTransform.append("\n<script language=\"JavaScript\" type=\"text/javascript\" src=\"" + sIntegradorPath + "\"></script>\n");
oPostTransform.append(sTransformed.substring(iCloseHead, iCloseHead+7));
oPostTransform.append(sTransformed.substring(iOpenBody, iCloseBody));
// Cargar el código fuente del control de visulización del Integrador
try {
sCharBuffer = oFS.readfilestr(sCtrlPath, "UTF-8");
if (DebugFile.trace) DebugFile.writeln(String.valueOf(sCharBuffer.length()) + " characters readed");
}
catch (com.enterprisedt.net.ftp.FTPException ftpe) {
throw new IOException (ftpe.getMessage());
}
try {
if (DebugFile.trace) DebugFile.writeln("Gadgets.replace(" + sCtrlPath + ",http://demo.hipergate.com/," + sWebServer + ")");
Gadgets.replace(sCharBuffer, "http://demo.hipergate.com/", sWebServer);
} catch (org.apache.oro.text.regex.MalformedPatternException e) { }
oPostTransform.append("<!--Begin " + sCtrlPath + "-->\n");
oPostTransform.append(sCharBuffer);
sCharBuffer = null;
oPostTransform.append("\n<!--End " + sCtrlPath + "-->\n");
oPostTransform.append(sTransformed.substring(iCloseBody));
}
else {
oPostTransform = new StringBuffer("Page " + oCurrentPage.getTitle() + " could not be rendered.");
if (oLastXcpt!=null) oPostTransform.append("<BR>" + oLastXcpt.getMessageAndLocation());
}
// Escribir el resultado con las llamadas incrustadas en el archivo de salida
if (sSelPageOptions.length()==0)
oFS.writefilestr(sOutputPath + oCurrentPage.getTitle().replace(' ','_') + "_." + sMedia, oPostTransform.toString(), "UTF-8");
else
try {
oFS.writefilestr(sOutputPath + oCurrentPage.getTitle().replace(' ','_') + "_." + sMedia, Gadgets.replace(oPostTransform.toString(), ":selPageOptions", sSelPageOptions), "UTF-8");
} catch (Exception e) {/* Ignore MalformedPatternException, is never thrown */ }
// Desreferenciar los buffers intermedios para liberar memoria lo antes posible
oPostTransform = null;