[Pfc-prolog-cvs] prolix/src/org/asturlinux/frade/prolix/web/servlets NodeDrawData.java,NONE,1.1 SvgG
Status: Beta
Brought to you by:
ivanfrade
From: <iva...@us...> - 2003-07-30 20:12:51
|
Update of /cvsroot/pfc-prolog/prolix/src/org/asturlinux/frade/prolix/web/servlets In directory sc8-pr-cvs1:/tmp/cvs-serv20789/src/org/asturlinux/frade/prolix/web/servlets Modified Files: TreeServlet.java Added Files: NodeDrawData.java SvgGenerator.java Log Message: Added auxiliar classes to draw svg in servlet --- NEW FILE: NodeDrawData.java --- package org.asturlinux.frade.prolix.web.servlets; import java.awt.Dimension; import java.awt.Point; public class NodeDrawData { public Dimension size; public Point origin; public NodeDrawData(Point originPoint, Dimension sizeBox) { origin = originPoint; size = sizeBox; } } --- NEW FILE: SvgGenerator.java --- package org.asturlinux.frade.prolix.web.servlets; import java.util.Iterator; import java.io.Writer; import java.io.IOException; //import java.io.FileWriter; //import java.io.FileReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.FactoryConfigurationError; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.*; import org.xml.sax.SAXException; import org.xml.sax.InputSource; import java.awt.Dimension; import java.awt.Point; import java.util.Vector; import java.io.StringWriter; import java.io.StringReader; import org.asturlinux.frade.prolix.web.servlets.NodeDrawData; public class SvgGenerator { /*************************** * Search methods ************************** */ /** * return nodes with "name" that have an "attribute" with defined "value" */ private Node[] search(Node root, String name, String attribute, String value) { Vector results = new Vector(); // Pass root element if (root.getNodeType() == Node.DOCUMENT_NODE) root = ((Document)root).getDocumentElement(); NodeList children = root.getChildNodes(); for ( int i = 0 ; i < children.getLength() ; i++) { Node currentNode = children.item(i); int nodeType = currentNode.getNodeType(); if (nodeType == Node.ELEMENT_NODE && currentNode.getNodeName().equals(name) && currentNode.hasAttributes()) { NamedNodeMap attMap = currentNode.getAttributes(); Node attrib = attMap.getNamedItem(attribute); if (attrib != null && attrib.getNodeValue().equals(value)) results.add(currentNode); } } //for //Not found return (Node [])results.toArray(new Node[results.size()]); } private Node searchNodeByNumber(Node root, String number) { if (root.getNodeType() == Node.DOCUMENT_NODE) root = ((Document)root).getDocumentElement(); NodeList children = root.getChildNodes(); for ( int i = 0 ; i < children.getLength() ; i++ ) { Node currentNode = children.item(i); int nodeType = currentNode.getNodeType(); if (nodeType == Node.ELEMENT_NODE && (currentNode.getNodeName().equals("node") || currentNode.getNodeName().equals("solution"))&& currentNode.hasAttributes()) { NamedNodeMap attMap = currentNode.getAttributes(); Node attribute = attMap.getNamedItem("number"); if (attribute != null && attribute.getNodeValue().equals(number)) return currentNode; } } // not found return null; } /** ******************************** * Draw methods * ******************************** */ public static final int marginX = 10; public static final int marginY = 5; public static final int nodeSizeX = 60; public static final int nodeSizeY = 40; private Dimension drawNode(Node raiz, String number, Point leftTopCorner, Writer output) { Node result = searchNodeByNumber(raiz,number); System.out.println("Looking for transitions"); Node[] result2 = search(raiz,"transition","origin",number); Dimension boundingBox = new Dimension(0,0); int childYBaseLine = leftTopCorner.y+marginY+nodeSizeY; //Vector childPoints = new Vector(); Vector childBoxes = new Vector(); for (int i = 0; i < result2.length ; i++) { String destiny = result2[i].getAttributes().getNamedItem("destiny").getNodeValue(); Point childPoint = new Point(leftTopCorner.x+ boundingBox.width, childYBaseLine); Dimension currentChildBoundingBox = drawNode(raiz,destiny,childPoint,output); childBoxes.add(new NodeDrawData(childPoint,currentChildBoundingBox)); boundingBox.width += currentChildBoundingBox.width + marginX; boundingBox.height = Math.max(boundingBox.height, currentChildBoundingBox.height); } boundingBox.width = Math.max(boundingBox.width, nodeSizeX); boundingBox.height += nodeSizeY + marginY; System.out.println("Aqui pinto la cajita"); Point nodePoint = new Point(boundingBox.width / 2 + leftTopCorner.x , leftTopCorner.y ); NodeDrawData[] destinations = (NodeDrawData[])childBoxes.toArray(new NodeDrawData[childBoxes.size()]); for (int i = 0; i < destinations.length ; i++) { imprimirTransicion(nodePoint,destinations[i],output); } imprimirNodo(result,nodePoint,output); return boundingBox; } private void imprimirTransicion(Point origen, NodeDrawData destino, Writer output) { try { output.write("<path d=\"M " + (origen.x + nodeSizeX/2) + " " + (origen.y+nodeSizeY) + " L " + (destino.origin.x + destino.size.width/2 + nodeSizeX/2) + " " + destino.origin.y + "\" \n"); output.write("id=\"" + origen.x*origen.y + "\"\n"); output.write("style=\"fill:none;fill-rule:evenodd;stroke:black;" + "stroke-opacity:1;stroke-width:0.5pt;stroke-linejoin:" + "miter;stroke-linecap:butt;fill-opacity:1;\"/>"); } catch (IOException e) { System.out.println("Error writing transition"); } } // And the refactoring winner is... private void imprimirNodo(Node node, Point leftTopPoint, Writer output) { // System.out.println("En el punto (" + leftTopPoint.x + "," // + leftTopPoint.y + ") imprimo el nodo " // + node.getAttributes().getNamedItem("number").getNodeValue()); try { output.write("<rect \n " + "width=\"" + nodeSizeX + "\" \n" + "height=\"" + nodeSizeY + "\" \n" + "x=\"" + leftTopPoint.x + "\" \n" + "y=\"" + leftTopPoint.y + "\" \n" + "id=\""+node.getAttributes().getNamedItem("number").getNodeValue()+"\" \n" + "style=\"fill:#00ca00;fill-opacity:1;\" " + "/> \n"); Node label = node.getAttributes().getNamedItem("label"); if (label != null) output.write("<text \n" + "x=\"" + (leftTopPoint.x + nodeSizeX/10) + "\" \n" + "y=\"" + (leftTopPoint.y + nodeSizeY/2 ) + "\" \n" + "id=\"text" + node.getAttributes().getNamedItem("number").getNodeValue() + "\">\n" + "<tspan id=\"" + node.getAttributes().getNamedItem("number").getNodeValue() + "\" >\n" + label.getNodeValue() + "</tspan>" + "</text>"); } catch (IOException e) { System.out.println("Error writing Node"); } } /** * Aux methods ***/ private void initWriter(Writer w) throws IOException { w.write("<?xml version=\"1.0\" " + "encoding=\"UTF-8\" standalone=\"no\"?>\n" + "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 20010904//EN\"\n" + "\"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">\n"); w.write("<svg xmlns=\"http://www.w3.org/2000/svg\"\n " + " xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n " + " version=\"1\"\n " + " x=\"30\" y=\"30\">\n "); } private void finishWriter(Writer w) throws IOException { w.write("\n</svg>\n"); } /** ************************************* * Main method ** ************************************* */ public String transform(String xmlString) { try { //FileReader xmlFile = new FileReader("/home/ivan/pfc/documentos/salida-prolix-treexml.xml"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); //Document document = builder.parse(new InputSource(xmlFile)); Document document = builder.parse(new InputSource(new StringReader(xmlString))); // Works with generic Writer. StringWriter svg = new StringWriter(); initWriter(svg); Dimension total = drawNode(document,"1",new Point(30,30),svg); System.out.println("Dimensiones totales: " + total.width + " " + total.height); finishWriter(svg); svg.close(); System.out.println("Pues no paso nada malo"); return svg.toString(); } catch (FactoryConfigurationError e) { // unable to get a document builder factory return null; } catch (ParserConfigurationException e) { // parser was unable to be configured return null; } catch (SAXException e) { // parsing error return null; } catch (IOException e) { System.out.println("I/O Error: File Not Exist or Problems with Writers"); return null; } } //public static void main(String argv[]) { // SvgGenerator on = new SvgGenerator(); // String cool = on.transform(); // System.out.println(cool); //} } Index: TreeServlet.java =================================================================== RCS file: /cvsroot/pfc-prolog/prolix/src/org/asturlinux/frade/prolix/web/servlets/TreeServlet.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** TreeServlet.java 27 Jul 2003 14:24:22 -0000 1.1 --- TreeServlet.java 30 Jul 2003 20:12:49 -0000 1.2 *************** *** 5,8 **** --- 5,9 ---- import javax.servlet.http.*; import org.asturlinux.frade.prolix.web.beans.CurrentStateBean; + import org.asturlinux.frade.prolix.web.servlets.SvgGenerator; public class TreeServlet extends HttpServlet *************** *** 17,35 **** CurrentStateBean bean = (CurrentStateBean)session.getAttribute("prologData"); ! // FIXME response.setContentType("image/svg+xml"); PrintWriter out = response.getWriter(); - // XML header - out.println("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"); - out.println("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 20010904//EN\" \"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">\n"); - - // SVG start - out.println("<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" version=\"1\" x=\"0\" y=\"0\" width=\"744.094\" height=\"1052.36\" id=\"svg101\">\n"); - - out.println("<defs id=\"defs103\" /> <rect width=\"244.357\" height=\"107.517\" x=\"87.9684\" y=\"120.549\" style=\"font-size:12;fill:#808080;fill-rule:evenodd;stroke-width:1pt;\" id=\"rect104\" />"); - out.println(bean.getCurrentResult()); - - // SVG stop - out.println("</svg> "); } } --- 18,27 ---- CurrentStateBean bean = (CurrentStateBean)session.getAttribute("prologData"); ! response.setContentType("image/svg+xml"); PrintWriter out = response.getWriter(); + SvgGenerator comeOn = new SvgGenerator(); + //FIXME Current Result can be null + out.println(comeOn.transform(bean.getCurrentResult())); } } |