package org.w3c.css.css; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.net.URL; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Enumeration; import java.util.HashMap; import java .util.Hashtable; import java.util.Iterator; import java.util.TimeZone; import java.util.Vector; import org.apache.velocity.VelocityContext; import org.apache.velocity.Template; import org.apache.velocity.app.Velocity; import org.apache.velocity.exception.ResourceNotFoundException; import org.apache.velocity.exception.ParseErrorException; import org.apache.velocity.exception.MethodInvocationException; import org.w3c.css.error.ErrorReportHTML; import org.w3c.css.parser.CssError; import org.w3c.css.parser.CssErrorToken; import org.w3c.css.parser.CssParseException; import org.w3c.css.parser.Errors; import org.w3c.css.properties.PropertiesLoader; import org.w3c.css.util.ApplContext; import org.w3c.css.util.InvalidParamException; import org.w3c.css.util.Messages; import org.w3c.css.util.Utf8Properties; import org.w3c.css.util.Warnings; /** * @author Julien Grand-Mourcel * @author Maria Kaceriakova * @date 2007-06-29 * This class uses Velocity to output the validator results * */ public class StyleSheetGenerator extends StyleReport { private PrintWriter out; private Template template; private String template_file; private String default_lang = "en"; private ApplContext ac; private StyleSheet style; private String title; private VelocityContext context; private Warnings warnings; private Errors errors; private Vector items; private static Utf8Properties availableFormat; private static Utf8Properties availablePropertiesURL; static { URL url; availableFormat = new Utf8Properties(); try { url = StyleSheetGenerator.class.getResource("format.properties"); java.io.InputStream f = url.openStream(); availableFormat.load(f); f.close(); } catch (Exception e) { System.err.println("org.w3c.css.css.StyleSheetGeneratorHTML: couldn't load format properties "); System.err.println(" " + e.toString()); } availablePropertiesURL = new Utf8Properties(); try { url = StyleSheetGenerator.class.getResource("urls.properties"); java.io.InputStream f = url.openStream(); availablePropertiesURL.load(f); f.close(); } catch (Exception e) { System.err.println("org.w3c.css.css.StyleSheetGeneratorHTML: couldn't load URLs properties "); System.err.println(" " + e.toString()); } } public StyleSheetGenerator(String title, StyleSheet style, String document, int warningLevel) { this(null, title, style, document, warningLevel); } public StyleSheetGenerator(ApplContext ac, String title, StyleSheet style, String document, int warningLevel) { this.ac = ac; this.style = style; this.title = title; this.template_file = availableFormat.getProperty(document); context = new VelocityContext(); context.put("file_title", title); // W3C_validator_result warnings = style.getWarnings(); errors = style.getErrors(); items = style.newGetRules(); // Setting all the variables of the velocity context ApplContext ac_default = new ApplContext(default_lang); String k; if (ac.getLang() == null || ac.getLang().equals(default_lang)) { Iterator it = ac_default.getMsg().properties.keySet().iterator(); while (it.hasNext()) { k = String.valueOf(it.next()); context.put(k, ac.getMsg().getString(k)); } } else { Iterator it = ac_default.getMsg().properties.keySet().iterator(); while (it.hasNext()) { k = String.valueOf(it.next()); if (ac.getMsg().getString(k) == null) context.put(k, ac_default.getMsg().getString(k)); else context.put(k, ac.getMsg().getString(k)); } } if (ac.getLink() != null) { HashMap[] languages = new HashMap[Messages.languages_name.size()]; String name; for (int i = 0; i < Messages.languages_name.size(); ++i) { name = String.valueOf(Messages.languages_name.get(i)); HashMap l = new HashMap(); l.put("name", name); l.put("real", ((Utf8Properties) Messages.languages.get(name)).getProperty("language_name")); languages[i] = l; } context.put("languages", languages); String link = ac.getLink().replaceAll("&lang=.*&", "&"); link = link.replaceAll("&lang=.*$", ""); context.put("link", "?" + link.replaceAll("&", "&")); } // generated values SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mss'Z'"); formatter.setTimeZone(TimeZone.getTimeZone("UTC")); context.put("currentdate", formatter.format(new Date())); context.put("lang", ac.getContentLanguage()); context.put("errors_count", new Integer(errors.getErrorCount())); context.put("warnings_count", new Integer(warnings.getWarningCount())); context.put("ignored-warnings_count", new Integer(warnings.getIgnoredWarningCount())); context.put("warning_level", new Integer(warningLevel)); context.put("rules_count", new Integer(items.size())); context.put("no_errors_report", new Boolean(false)); context.put("charset", ac.getContentEncoding()); context.put("cssversion", ac.getCssVersion()); context.put("css", ac.getMsg().getString(ac.getCssVersion())); context.put("css_link", getURLProperty("@url-base_"+ac.getCssVersion())); context.put("is_valid", (errors.getErrorCount() == 0) ? "true" : "false"); context.put("author", "www-validator-css"); context.put("author-email", "Email.html"); if (style.charset != null) context.put("style_charset", style.charset); produceError(); produceWarning(); produceStyleSheet(); try { Velocity.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, this.getClass().getResource("").getPath()); Velocity.init(); template = Velocity.getTemplate(template_file); } catch (ResourceNotFoundException rnfe) { System.err.println(rnfe.getMessage()); rnfe.printStackTrace(); } catch (ParseErrorException pee) { System.err.println(pee.getMessage()); pee.printStackTrace(); } catch (MethodInvocationException mie) { System.err.println(mie.getMessage()); mie.printStackTrace(); } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(); } } private final static String getURLProperty(String name) { return availablePropertiesURL.getProperty(name); } public final static void printAvailableFormat(PrintWriter out) { Enumeration e = availableFormat.propertyNames(); out.println(" -- listing available output format --"); while (e.hasMoreElements()) { String key = ((String) e.nextElement()).toLowerCase(); out.println("Format : " + key); out.println(" File : " + getDocumentName(key)); } out.flush(); } /** * Test if document is an available output * @param document, the desired output * @return true if the desired output is available */ public static boolean isAvailableFormat(String document) { return availableFormat.containsKey(document); } private final static String getDocumentName(String documentName) { String document = availableFormat.getProperty(documentName .toLowerCase()); if (document == null) { System.err.println("Unable to find " + documentName.toLowerCase() + " output format"); return documentName; } else { return document; } } /** * Add the style information to the context */ private void produceStyleSheet() { context.put("at_rules_list", style.newGetRules()); } /** * Add the errors information to the context * For each error, *