/* * The contents of this file are subject to the Mozilla Public License Version 1.1 * (the "License"); you may not use this file except in compliance with the License. * You may obtain a copy of the License at . * * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT * WARRANTY OF ANY KIND, either express or implied. See the License for the specific * language governing rights and limitations under the License. * * The Original Code is the Venice Web Communities System. * * The Initial Developer of the Original Code is Eric J. Bowersox , * for Silverwrist Design Studios. Portions created by Eric J. Bowersox are * Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved. * * Contributor(s): */ package com.silverwrist.venice.servlets.format; import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import javax.xml.parsers.*; import org.apache.log4j.*; import org.w3c.dom.*; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import com.silverwrist.util.DOMElementHelper; import com.silverwrist.util.StringUtil; import com.silverwrist.venice.core.ConfigException; import com.silverwrist.venice.core.UserContext; import com.silverwrist.venice.servlets.Variables; public class RenderConfig { /*-------------------------------------------------------------------------------- * Static data values *-------------------------------------------------------------------------------- */ protected static final String ATTR_NAME = "com.silverwrist.venice.servlets.RenderConfig"; protected static final String CONFIG_FILE_PARAM = "render.config"; private static Category logger = Category.getInstance(RenderConfig.class.getName()); /*-------------------------------------------------------------------------------- * Attributes *-------------------------------------------------------------------------------- */ private Document config; private String site_title; private boolean want_comments; private boolean allow_gzip; private String font_face; private String image_url; private String static_url; private String site_logo; private Hashtable stock_messages; /*-------------------------------------------------------------------------------- * Constructor *-------------------------------------------------------------------------------- */ protected RenderConfig(String config_file) throws ConfigException { config = loadConfiguration(config_file); // Make sure the configuration is valid... Element root = config.getDocumentElement(); if (!(root.getTagName().equals("render-config"))) { // not the correct root tag name logger.fatal("config document is not a document (root tag: <" + root.getTagName() + "/>)"); throw new ConfigException("document is not a document",root); } // end if // Get the site name. DOMElementHelper root_h = new DOMElementHelper(root); site_title = root_h.getSubElementText("site-name"); if (site_title==null) { // no section - bail out now! logger.fatal("config document has no element"); throw new ConfigException("no section found in config file",root); } // end if if (logger.isDebugEnabled()) logger.debug("Site title: " + site_title); Element render_sect = root_h.getSubElement("rendering"); if (render_sect==null) { // no section - bail out now! logger.fatal("config document has no section"); throw new ConfigException("no section found in config file",root); } // end if DOMElementHelper render_sect_h = new DOMElementHelper(render_sect); want_comments = render_sect_h.hasChildElement("html-comments"); allow_gzip = render_sect_h.hasChildElement("gzip-output"); if (logger.isDebugEnabled()) { // log the read values logger.debug("Use HTML comments: " + String.valueOf(want_comments)); logger.debug("Use GZIP encoding: " + String.valueOf(allow_gzip)); } // end if font_face = render_sect_h.getSubElementText("font"); if (font_face==null) { // no tag - bail out now! logger.fatal(" section has no element"); throw new ConfigException("no found in section",render_sect); } // end if if (logger.isDebugEnabled()) logger.debug("Font face: " + font_face); Element paths_sect = root_h.getSubElement("paths"); if (paths_sect==null) { // no section - bail out now! logger.fatal("config document has no section"); throw new ConfigException("no section found in config file",root); } // end if DOMElementHelper paths_sect_h = new DOMElementHelper(paths_sect); image_url = paths_sect_h.getSubElementText("image"); if (image_url==null) { // no tag - bail out now! logger.fatal(" section has no element"); throw new ConfigException("no found in section",paths_sect); } // end if if (logger.isDebugEnabled()) logger.debug("Image path: " + image_url); static_url = paths_sect_h.getSubElementText("static"); if (static_url==null) { // no tag - bail out now! logger.fatal(" section has no element"); throw new ConfigException("no found in section",paths_sect); } // end if if (logger.isDebugEnabled()) logger.debug("Static files path: " + static_url); site_logo = paths_sect_h.getSubElementText("site-logo"); if (site_logo==null) { // no tag - bail out now! logger.fatal(" section has no element"); throw new ConfigException("no found in section",paths_sect); } // end if if (logger.isDebugEnabled()) logger.debug("Site logo: " + image_url); Element msg_sect = root_h.getSubElement("messages"); if (msg_sect==null) { // no section - bail out now! logger.fatal("config document has no section"); throw new ConfigException("no section found in config file",root); } // end if // Initialize the stock messages list. stock_messages = new Hashtable(); NodeList msg_nodes = msg_sect.getChildNodes(); for (int i=0; i").append(specific).append(" - ").append(site_title).append(""); return buf.toString(); } // end getTitleTag String getSiteImageTag(int hspace, int vspace) { StringBuffer buf = new StringBuffer(); buf.append("\"").append(site_title);0) buf.append(" HSPACE=").append(hspace); if (vspace>0) buf.append(" VSPACE=").append(vspace); buf.append('>'); return buf.toString(); } // end getSiteImageTag String getStdFontTag(String color, int size) { StringBuffer buf = new StringBuffer("'); return buf.toString(); } // end getStdFontTag String getStdBaseFontTag(int size) { StringBuffer buf = new StringBuffer("*"); return buf.toString(); } // end getRequiredBullet void writeFooter(Writer out) throws IOException { out.write("
\n" + "\n\n\n" + "
\n"); out.write(getStdFontTag(null,1)); out.write(getStockMessage("footer-text")); out.write("\n\n" + "\"Powered\n
\n"); } // end writeFooter void writeContentHeader(Writer out, String primary, String secondary) throws IOException { out.write(getStdFontTag("#3333AA",5) + "" + StringUtil.encodeHTML(primary) + "
"); if (secondary!=null) out.write("  " + getStdFontTag("#3333AA",3) + "" + StringUtil.encodeHTML(secondary) + "
"); out.write("
\n"); } // end writeContentHeader String getStockMessage(String identifier) { return (String)(stock_messages.get(identifier)); } // end getStockMessage void writeStockMessage(Writer out, String identifier) throws IOException { String text = (String)(stock_messages.get(identifier)); out.write(StringUtil.encodeHTML(text)); } // end writeStockMessage /*-------------------------------------------------------------------------------- * Static operations for use by VeniceServlet *-------------------------------------------------------------------------------- */ public static RenderConfig getRenderConfig(ServletContext ctxt) throws ServletException { // Look in the servlet attributes first. Object obj = ctxt.getAttribute(ATTR_NAME); if (obj!=null) return (RenderConfig)obj; // Get the parameter for the renderer's config file. String cfgfile = ctxt.getInitParameter(CONFIG_FILE_PARAM); logger.info("Initializing Venice rendering using config file: " + cfgfile); try { // create the RenderConfig object and save it to attributes. RenderConfig rconf = new RenderConfig(cfgfile); ctxt.setAttribute(ATTR_NAME,rconf); return rconf; } // end try catch (ConfigException e) { // configuration failed! post an error message logger.fatal("Rendering configuration failed: " + e.getMessage(),e); throw new ServletException("Venice rendering configuration failed: " + e.getMessage(),e); } // end catch } // end getRenderConfig public static RenderData createRenderData(ServletContext ctxt, HttpServletRequest request, HttpServletResponse response) throws ServletException { UserContext uc = Variables.getUserContext(ctxt,request,request.getSession(true)); return new RenderData(getRenderConfig(ctxt),uc,request,response); } // end createRenderData public static RenderData createRenderData(ServletContext ctxt, UserContext uc, HttpServletRequest request, HttpServletResponse response) throws ServletException { return new RenderData(getRenderConfig(ctxt),uc,request,response); } // end createRenderData } // end class RenderConfig