80 likes | 215 Views
XML-XSL Wrapper für Java der Prototyp:. XXWrapper ParserTest PicOut xslFile.xsl xmlFile.xml. public class XXWrapper { private final static String PARSER_CLASS ="com.jclark.xml.sax.Driver"; StringWriter XMLWriter=null; String stylesheet="";
E N D
XML-XSL Wrapper für Javader Prototyp: • XXWrapper • ParserTest • PicOut • xslFile.xsl • xmlFile.xml
public class XXWrapper { private final staticString PARSER_CLASS ="com.jclark.xml.sax.Driver"; StringWriter XMLWriter=null; String stylesheet=""; public XXWrapper(String stylesheet, StringWriter XMLWriter) { this.stylesheet=stylesheet; this.XMLWriter=XMLWriter; } publicString parse() throwsServletException, IOException { XSLProcessor xslProcessor = createXSLProcessor(stylesheet); StringReader reader = newStringReader(XMLWriter.toString()); File temp = transformAndWrite(xslProcessor,reader); return setFileToString(temp); } publicString setFileToString(File temp) throwsServletException { String s=""; String output=""; try { BufferedReader reader = newBufferedReader(newFileReader(temp)); while((s=reader.readLine()) != null) { output = output + s + "\n"; } } catch (Exception e) { throw newServletException(e.toString()); } return output; } • XXWrapper • ParserTest • PicOut • xslFile.xsl • xmlFile.xml
protectedXSLProcessor createXSLProcessor(String url)throwsServletException, IOException {Parser parser = null;try {Class clasz = Class.forName(PARSER_CLASS); parser = (Parser)clasz.newInstance(); }catch(ClassNotFoundException e) {throw newServletException(e.toString()); }catch(InstantiationException e) {throw newServletException(e.toString()); }catch(IllegalAccessException e) {throw newServletException(e.toString()); }XSLProcessor xslProcessor = new XSLProcessorImpl(); xslProcessor.setParser(parser);Reader XSLReader = newFileReader(url);InputSource inputSource = newInputSource(XSLReader);try { xslProcessor.loadStylesheet(inputSource); }catch (SAXException e) {throw newServletException(e.toString()); }return xslProcessor; } • XXWrapper • ParserTest • PicOut • xslFile.xsl • xmlFile.xml
protectedFile transformAndWrite(XSLProcessor xslProcessor, Reader reader)throwsServletException, IOException {OutputMethodHandlerImpl outputMethodHandler = new OutputMethodHandlerImpl(xslProcessor); xslProcessor.setOutputMethodHandler(outputMethodHandler);File temp = File.createTempFile("xxwrapper", ".tmp"); outputMethodHandler.setDestination(new FileDestination(temp));try { xslProcessor.parse(new InputSource(reader)); }catch (Exception e) {throw new ServletException(e.toString()); }return temp; }} • XXWrapper • ParserTest • PicOut • xslFile.xsl • xmlFile.xml
publicclass ParserTest extendsHttpServlet { StringWriter XMLWriter=null; String output = ""; publicvoid doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException { response.setContentType("text/html"); PrintWriter writer = response.getWriter(); XMLWriter = new StringWriter(); try { setXMLData(new File("/home/httpd/htdocs/servlets/" + "de/wpp/xml/xmlFile.xml"), XMLWriter); } catch (Exception ioe) { log(ioe.toString()); } XXWrapper wrapper = new XXWrapper("/home/httpd/htdocs/" + "servlets/de/wpp/xml/xslFile.xsl", XMLWriter); output=wrapper.parse(); writer.println(output); } protectedvoid setXMLData(File xmlFile, Writer w) throwsIOException { String s=""; BufferedReader r = new BufferedReader(new FileReader(xmlFile)); while ((s=r.readLine()) != null) { w.write(s); } w.flush(); } } • XXWrapper • ParserTest • PicOut • xslFile.xsl • xmlFile.xml
publicclass PicOut extendsHttpServlet { protectedvoid doGet(HttpServletRequest request,HttpServletResponse response) throwsServletException,IOException { PrintWriter writer=response.getWriter(); response.setContentType("image/gif"); try { int bufferSize=65535; File pic = new File(request.getParameter("filename")); InputStreamReader picStream = new InputStreamReader(new FileInputStream(pic)); char buf[] = new char[bufferSize]; long size=pic.length(); while (size>0) { if (size<bufferSize) { bufferSize=(int)size; } int b=picStream.read(buf,0,bufferSize); writer.write(buf,0,bufferSize); size=size-bufferSize; } picStream.close(); } catch (IOException e) { writer.println("Bild nicht gefunden"); } writer.close(); } } • XXWrapper • ParserTest • PicOut • xslFile.xsl • xmlFile.xml
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns="http://www.w3.org/TR/REC-html40" result-ns=""> <xsl:outputmethod="html"/> <xsl:templatematch="/"> <HTML><HEAD> <TITLE>XML/XSL-Testseite</TITLE> <link rel="stylesheet" type="text/css" href="http://139.6.67.191/~hebtyp/hebtyp.css"/> </HEAD> <BODY bgcolor="#ffffff"> <div align="center"><font size="6">XML/XSL Test</font><br/><br/> <table width="90%" border="0"> <tr><td colspan="5">Hier werden einige Daten mit Hilfe der XSL/XML-Technologie dargestellt. Etwas Text und dann auch einige GIFs.<br/> <br/> </td></tr> <xsl:for-eachselect="liste/item"> <TR> <TD bgcolor="#dddddd"><xsl:value-ofselect="vorname"/> </TD> <TD bgcolor="#dddddd"><xsl:value-ofselect="name"/> </TD> <TD bgcolor="#dddddd"><a><xsl:attributename="href">mailto:<xsl:value-ofselect="email"/></xsl:attribute> <xsl:value-ofselect="email"/> </a></TD> <TD bgcolor="#dddddd"><a><xsl:attributename="href"> <xsl:value-ofselect="homepage"/></xsl:attribute> <xsl:value-ofselect="homepage"/> </a></TD> <TD bgcolor="#dddddd"><img><xsl:attributename="src"> http://139.6.67.191/servlets/de.wpp.xml.PicOut?filename=<xsl:value-ofselect="image"/></xsl:attribute></img></TD> </TR> </xsl:for-each> </table><br/><br/>(c) SoeBa Inc.</div></BODY></HTML> </xsl:template> </xsl:stylesheet> • XXWrapper • ParserTest • PicOut • xslFile.xsl • xmlFile.xml
<?xml version="1.0" encoding="ISO-8859-1"?> <liste> <itemid="1"> <vorname>Nicole</vorname> <name>Bachner</name> <email>bachner@at12.de</email> <homepage>http://www.need-speed.de</homepage> <image>/home/httpd/htdocs/chet.gif</image> </item> <itemid="2"> <vorname>Michael</vorname> <name>Soemers</name> <email>michael@soemers.de</email> <homepage>http://www.soemers.de</homepage> <image>/home/httpd/htdocs/bet.gif</image> </item> <itemid="3"> <vorname>Magnus</vorname> <name>Paprotta</name> <email>magnus_paprotta@gmx.de</email> <homepage>http://www.gm.fh-koeln.de/~ia194</homepage> <image>/home/httpd/htdocs/ajin.gif</image> </item> </liste> • XXWrapper • ParserTest • PicOut • xslFile.xsl • xmlFile.xml