// Imported TraX classes import javax.xml.transform.*; import javax.xml.transform.dom.*; import javax.xml.transform.stream.*; // Imported SAX classes import org.xml.sax.*; import org.xml.sax.helpers.*; // Imported DOM classes import org.w3c.dom.*; import org.w3c.dom.traversal.*; // Imported java.io classes import java.io.*; import java.util.*; import java.text.*; // Imported JAVA API for XML Parsing classes import javax.xml.parsers.*; // Apache Xalan classes import org.apache.xpath.XPathAPI; import org.apache.xalan.serialize.*; import org.apache.xalan.templates.OutputProperties; class Thema2html { File outputDir = new File("html"); File themaFile = null; File expandedFile; File expandScript; File styleScript; TransformerFactory tFactory = TransformerFactory.newInstance(); DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder, dBuilderNonValidating; boolean forceExpansion = false; public Thema2html() throws Exception { expandedFile = new File("expanded.xml"); expandScript = new File("../xsl/themaShelf/expand.xsl"); styleScript = new File("../xsl/style-lexicon/style-lexicon.xsl"); dFactory.setValidating(true); dBuilder = dFactory.newDocumentBuilder(); dFactory.setValidating(false); dBuilderNonValidating = dFactory.newDocumentBuilder(); } /* Member class View */ private class View { public String viewName; public boolean eachItem = false; public String paramName = ""; public Set paramValues = new HashSet(); public View(Node viewNode) throws TransformerException { NamedNodeMap attr = viewNode.getAttributes(); viewName = attr.getNamedItem("name").getNodeValue(); if(attr.getNamedItem("each-item")!=null) eachItem = attr.getNamedItem("each-item").getNodeValue()!=""; Node paramNode = attr.getNamedItem("param"); if (paramNode != null) { paramName = paramNode.getNodeValue(); NodeIterator values = XPathAPI.selectNodeIterator(viewNode,"view:value/text()"); Node n; while((n=values.nextNode())!=null) { paramValues.add(n.getNodeValue()); } } } public String toString() { String s = "Stylesheet.View '"+viewName+"':\n -"; s += eachItem ? " one page for each term" : " single page"; s += eachItem; if(paramName!="") { s += "\n - parameter '"+paramName+"': ["; for (Iterator i = paramValues.iterator() ; i.hasNext() ;) { s += (String)i.next(); if (i.hasNext()) s+= ", "; } s += "]"; } return s; } } /* */ public void message(String msg) { System.out.println(msg); } public DOMSource createDOMSource(File xmlFile, boolean validate) throws SAXException, IOException { message("load "+xmlFile); //StringBuffer errorBuff = new StringBuffer(); //Handler handler = new Handler(errorBuff); Document d; if(validate) { //dBuilder.setErrorHandler(handler); d = dBuilder.parse(xmlFile); //if(handler.hasError) throw new SAXException("File "+xmlFile+" is not valid!"+errorBuff.toString()); } else { //dBuilderNonValidating.setErrorHandler(handler); d = dBuilderNonValidating.parse(xmlFile); //if(handler.hasError) throw new SAXException("File "+xmlFile+" is not well-formed!"+errorBuff.toString()); } return new DOMSource(d,xmlFile.getCanonicalPath()); } public void action() throws Exception { DOMSource themaDOM = null; NodeList conceptIds; String rootId; // expand if (expandedFile.exists() && expandedFile.lastModified() > themaFile.lastModified() &&! forceExpansion) { message("Loading expanded: " + expandedFile); themaDOM = createDOMSource(expandedFile,false); themaDOM.setSystemId(themaFile.getCanonicalPath()); } else { /*ThemaReader r; if(XMLReading.isXMLFile(location)) { r = new ThemaSimpleReader(new TemplatesRepository("../xsl/")); } else { r = new PlainTextReader(); } themaDOM = new DOMSource(r.read(location),location.getCanonicalPath()); */ Transformer expandTransformer = tFactory.newTransformer(new StreamSource(expandScript)); DOMResult expanded = new DOMResult(); Source themaSource = new StreamSource(themaFile); message("Expanding " + themaFile + " to " + expandedFile); expandTransformer.transform(themaSource,expanded); message("Expanding done"); //themaDOM = new DOMSource(expanded.getNode(),themaDOM.getSystemId()); themaDOM = new DOMSource(expanded.getNode(),themaFile.getCanonicalPath()); // Save expanded as xml file message("Saving " + expandedFile); Properties outputFormat = OutputProperties.getDefaultMethodProperties("xml"); outputFormat.setProperty("encoding","ISO-8859-1"); Serializer serializer = SerializerFactory.getSerializer(outputFormat); serializer.setWriter(new FileWriter(expandedFile)); serializer.asDOMSerializer().serialize(themaDOM.getNode()); } // Analysing thema //String xpath = "//concept[@id]|//thema[@id]"; String xpath = "//concept/@id|//thema/@id"; //if (requestId!="") xpath += "[.='"+requestId+"']"; conceptIds = XPathAPI.selectNodeList(themaDOM.getNode(),xpath); rootId = XPathAPI.selectSingleNode(themaDOM.getNode(),"/thema/@id").getNodeValue(); message(conceptIds.getLength() + " concepts found. Root is '"+rootId+"'"); // load stylesheet message("Load stylesheet"); Source xslSource = new StreamSource( styleScript ); DOMSource styleDOM = createDOMSource(styleScript,false); Transformer styleTransformer = tFactory.newTransformer( xslSource ); // extract Views xpath = "/stylesheet/view|/xsl:stylesheet/view:view"; //if(requestView!="") xpath += "[@name='"+requestView+"']"; NodeList views = XPathAPI.selectNodeList(styleDOM.getNode(),xpath); View[] styleViews = new View[views.getLength()]; for(int v=0; v outputFile.lastModified()) { //transformDOM2File(inDOM,outFile); styleTransformer.transform(themaDOM,new StreamResult(outputFile)); } } } } private boolean parseArgs(String [] args) { for(int i=0; i1) ? arg.charAt(1) : 0; if(arg.charAt(0)=='-' && c!=0) { // flag (starting with '-') String value = ""; if(arg.length()>2) value = arg.substring(2); else value = i