/** * Generates the HTML for a Method element. * * @param layout A Method Element. Layout object. * @return A url of the generated content HTML file. */ private StringBuffer getXml(IElementLayout layout) { StringBuffer xml = new StringBuffer(); XmlElement xmlElement = layout.getXmlElement(true); // set the language attribute Locale locale = Locale.getDefault(); String lang = locale.getLanguage(); xmlElement.setAttribute("lang", lang); // $NON-NLS-1$ if (showTreeBrowser) { xmlElement.setAttribute("showTreeBrowser", "true"); // $NON-NLS-1$ //$NON-NLS-2$ } xml.append(XmlHelper.XML_HEADER).append(xmlElement.toXml()); // if ( debug || CommandLineRunUtil.getInstance().isNeedToRun()) { if (debug) { try { String xml_file = this.getPublishDir() + "xml" + File.separator; // $NON-NLS-1$ xml_file += layout.getType() + "." + layout.getFileName(".xml"); // $NON-NLS-1$ //$NON-NLS-2$ File xf = new File(xml_file); if (!xf.exists()) { xf.getParentFile().mkdirs(); xf.createNewFile(); } OutputStreamWriter output = new OutputStreamWriter(new FileOutputStream(xf), "utf-8"); // $NON-NLS-1$ output.write(xml.toString()); output.flush(); output.close(); // FileWriter xw = new FileWriter(xml_file); // xw.write(xml.toString()); // xw.flush(); // xw.close(); } catch (Exception ex) { ex.printStackTrace(); } } return xml; }
/** * Generates the HTML for a Method element. * * @param layout A Method Element. Layout object. * @param xslURI The XSL stylesheet. * @param htmlFile The HTML file to be generated. * @param xml The XML buffer. * @return A url of the generated content HTML file. */ private void generateHtml( IElementLayout layout, String xsl_uri, String html_file, StringBuffer xml) { Throwable th = null; if (layout == null) { return; } Timer timer = null; if (debug) { timer = new Timer(); } try { StringWriter sw = new StringWriter(); if (getValidator().showLinkedPageForDescriptor() && layout instanceof DescriptorLayout) { DescriptorLayout dLayout = (DescriptorLayout) layout; MethodElement linedElement = dLayout.getLinkedElement(); if (linedElement != null) { getValidator().addReferencedElement(dLayout.getElement(), linedElement); return; } } XSLTProcessor.transform(xsl_uri, xml.toString(), xslParams, sw); sw.flush(); String content = sw.getBuffer().toString(); if (debug) { timer.stop(); System.out.println( timer.getTime() + " mini seconds for xml/xslt transformation"); // $NON-NLS-1$ timer.start(); } // Always validate and fix the content before publishing. content = ResourceHelper.validateContent( layout.getElement(), content, getValidator(), layout.getLayoutMgr().getConfiguration(), this.layoutXslRootPath); if (contentScanEnabled()) { // scanContentForResources(layout.getElement(), content, layout // .getFilePath()); getValidator().scanContent(layout, content); } content = getLayoutManager().getAdjustedElementPathStringValue(content); if (elementContentMap != null) { elementContentMap.put(layout.getElement(), content); } if (debug) { timer.stop(); System.out.println(timer.getTime() + " mini seconds scanning content"); // $NON-NLS-1$ timer.start(); } OutputStreamWriter output = new OutputStreamWriter(new FileOutputStream(html_file), "utf-8"); // $NON-NLS-1$ output.write(content); output.flush(); output.close(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); th = e; } catch (FileNotFoundException e) { e.printStackTrace(); th = e; } catch (IOException e) { e.printStackTrace(); th = e; } catch (Exception e) { e.printStackTrace(); th = e; } if (th != null) { this.getValidator() .logError(layout.getElement(), "Error generating element content", th); // $NON-NLS-1$ } }
/** * Generates the HTML for a Method element. * * @param layout A Method Element. Layout object. * @param linkedElements If not null, this object will be populated with a list of linked elements * in the page. * @return A url of the generated content HTML file. */ public String generateHtml(IElementLayout layout) { if (layout == null) { return "about:blank"; //$NON-NLS-1$ } // System.out.println("*** generateHtml: " + LibraryUtil.getTypeName(layout.getElement()) ); // add time logging when publishing element long time_start = Calendar.getInstance().getTimeInMillis(); String elementPath = layout.getNoAdjustedElementPath().replace('/', File.separatorChar); String elementPathName = elementPath + layout.getFileName(ResourceHelper.FILE_EXT_HTML); String filePath = this.getPublishDir() + elementPath; String html_file = this.getPublishDir() + elementPathName; try { StringBuffer xml = getXml(layout); String xsl_uri; File f = new File(filePath); if (!f.exists()) { f.mkdirs(); } // Generate the additonal outputs. List layouts = layout.getLayouts(); xsl_uri = layoutXslRootPath + layout.getXslUrl(); generateHtml(layout, xsl_uri, html_file, xml); // Generate other layout files. if (layouts != null && layouts.size() > 0) { for (Iterator it = layouts.iterator(); it.hasNext(); ) { LayoutInfo info = (LayoutInfo) it.next(); xsl_uri = layoutXslRootPath + info.layout_xsl; String file = filePath + info.fileName; generateHtml(layout, xsl_uri, file, xml); } } } catch (Exception e) { e.printStackTrace(); html_file = null; } long time_end = Calendar.getInstance().getTimeInMillis(); long mini_seconds = time_end - time_start; if (mini_seconds > 1000) { String msg = mini_seconds + " mini-second(s) publishing element " + LibraryUtil.getTypeName(layout.getElement()) + "[" + elementPathName + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ IContentValidator validator = getValidator(); if (validator == null) { System.out.println(msg); } else { validator.logInfo(msg); } } if (html_file != null) { return html_file; } return "about:blank"; //$NON-NLS-1$ }