[Pfc-prolog-cvs] prolix/src/org/asturlinux/frade/prolix/web/servlets SvgGenerator.java,1.6,1.7
Status: Beta
Brought to you by:
ivanfrade
From: <iva...@us...> - 2003-09-11 16:41:27
|
Update of /cvsroot/pfc-prolog/prolix/src/org/asturlinux/frade/prolix/web/servlets In directory sc8-pr-cvs1:/tmp/cvs-serv6216 Modified Files: SvgGenerator.java Log Message: Fixed some words at spanish. Now traslated to english - Passed astyle Index: SvgGenerator.java =================================================================== RCS file: /cvsroot/pfc-prolog/prolix/src/org/asturlinux/frade/prolix/web/servlets/SvgGenerator.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** SvgGenerator.java 22 Aug 2003 11:31:39 -0000 1.6 --- SvgGenerator.java 11 Sep 2003 16:41:15 -0000 1.7 *************** *** 34,94 **** * 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 --- 34,88 ---- * 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 *************** *** 102,328 **** public static final String solutionNodeColor = "#d8d600"; ! private Dimension mySize(Node node) ! { ! Dimension result = new Dimension(nodeSizeX,nodeSizeY); ! if (node.getAttributes() == null) ! return result; ! ! if (node.getNodeName().equals("solution")) ! { ! //Node[] subst = filterSubstitutionNodes(node.getChildNodes()); ! //int numberOfSubst = subst.length; ! return result; ! } ! Node label = node.getAttributes().getNamedItem("label"); ! if (label != null) ! { ! String text = label.getNodeValue(); ! int thisNodeXSize = Math.max(text.length() * 7, nodeSizeX); ! result.width = thisNodeXSize; ! } ! return result; } ! private Dimension drawNode(Node raiz, String number, ! Point leftTopCorner, ! Writer output) ! { ! Node result = searchNodeByNumber(raiz,number); ! ! //System.out.println("Looking for transitions"); ! Node[] transitions = search(raiz,"transition","origin",number); ! Dimension boundingBox = new Dimension(0,0); ! int childYBaseLine = leftTopCorner.y + marginY + nodeSizeY; ! Vector childBoxes = new Vector(); ! for (int i = 0; i < transitions.length ; i++) ! { ! String destiny = transitions[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, ! mySize(result).width); ! boundingBox.height += nodeSizeY + marginY; ! // System.out.println("Aqui pinto la cajita"); ! Point nodePoint = new Point((boundingBox.width - marginX)/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 // + currentNodeSizeX / 2 ! + " " + (origen.y+nodeSizeY) ! + " L " ! + (destino.origin.x + destino.size.width/2) ! + " " + destino.origin.y + "\" \n"); ! output.write("id=\"" + idGenerator.createTransitionId() ! + "\"\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"); ! } } private void printConventionalNode(Node node, Point leftTopPoint, ! Writer output) ! { ! try { ! int currentNodeSizeX = mySize(node).width; ! //the poing i receive is really de middle of boundingBox ! output.write("<rect \n " ! + " width=\"" + currentNodeSizeX + "\" \n" ! + " height=\"" + nodeSizeY + "\" \n" ! + " x=\"" + (leftTopPoint.x - (currentNodeSizeX / 2)) + "\" \n" ! + " y=\"" + leftTopPoint.y + "\" \n" ! + " id=\""+node.getAttributes().getNamedItem("number").getNodeValue()+"\" \n" ! + " style=\"fill:" + conventionalNodeColor ! + ";fill-opacity:1;\" " ! + " /> \n"); ! Node label = node.getAttributes().getNamedItem("label"); ! if (label != null) ! output.write("<text \n" ! + " x=\"" + (leftTopPoint.x - currentNodeSizeX/2.5) + "\" \n" ! + " y=\"" + (leftTopPoint.y + nodeSizeY/2 ) + "\" \n" ! + " id=\"" + idGenerator.createTextId() ! + "\">\n" ! + " <tspan id=\"" + idGenerator.createTextId() ! + "\" >\n " ! + label.getNodeValue() ! + "\n </tspan>\n" ! + "</text>"); - } catch (IOException e) { - System.out.println("Error writing Node"); - } - } ! private Node[] filterSubstitutionNodes(NodeList original) ! { ! Vector results = new Vector(); ! int elements = original.getLength(); ! for (int i = 0; i < elements; i++) { ! Node currentNode = original.item(i); ! int nodeType = currentNode.getNodeType(); ! if (nodeType == Node.ELEMENT_NODE && ! currentNode.getNodeName().equals("substitution")) ! results.add(currentNode); ! } ! return (Node [])results.toArray(new Node[results.size()]); } private void printSolutionNode(Node node, Point leftTopPoint, ! Writer output) ! { ! try ! { ! int currentNodeSizeX = mySize(node).width; ! output.write("<rect \n " ! + " width=\"" + currentNodeSizeX + "\" \n" ! + " height=\"" + nodeSizeY + "\" \n" ! + " x=\"" + (leftTopPoint.x - (currentNodeSizeX / 2)) + "\" \n" ! + " y=\"" + leftTopPoint.y + "\" \n" ! + " id=\"" +node.getAttributes().getNamedItem("number").getNodeValue()+"\" \n" ! + " style=\"fill:" + solutionNodeColor ! +";fill-opacity:1;\" " ! + " /> \n"); ! Node[] subst = filterSubstitutionNodes(node.getChildNodes()); ! int numberOfSubst = subst.length; - float substYPoint = leftTopPoint.y + nodeSizeY/2; - for (int i = 0; i < numberOfSubst; i++) - { - output.write("<text \n" - + " x=\"" + (leftTopPoint.x - currentNodeSizeX/2.5) + "\" \n" - + " y=\"" + (substYPoint + 12*i) + "\" \n" - + " id=\"" + idGenerator.createTextId() - + "\">\n" - + " <tspan id=\"" + idGenerator.createTextId() - + "\" >\n " - + subst[i].getAttributes().getNamedItem("variable").getNodeValue() - + " : " - + subst[i].getAttributes().getNamedItem("value").getNodeValue() - + "\n </tspan>\n" - + "</text>"); - } - } catch (IOException io) { - System.out.println("Error writing Solution node"); - } - } // 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.getNodeName()); ! // + node.getAttributes().getNamedItem("number").getNodeValue()); ! if (node.getNodeName().equals("solution")) ! printSolutionNode(node, leftTopPoint, output); ! else ! printConventionalNode(node, leftTopPoint, output); } ! /** * Aux methods ***/ private String svgInit(int height, int width) ! throws IOException ! { ! StringWriter w = new StringWriter(); ! 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 " ! + " height=\"" + height + "\" width=\"" + width + "\"> \n"); ! return w.toString(); } ! private String svgEnd() ! throws IOException ! { ! return new String("\n</svg>\n"); } --- 96,305 ---- public static final String solutionNodeColor = "#d8d600"; ! private Dimension mySize(Node node) { ! Dimension result = new Dimension(nodeSizeX,nodeSizeY); ! if (node.getAttributes() == null) ! return result; ! if (node.getNodeName().equals("solution")) { ! //Node[] subst = filterSubstitutionNodes(node.getChildNodes()); ! //int numberOfSubst = subst.length; ! return result; ! } ! ! Node label = node.getAttributes().getNamedItem("label"); ! if (label != null) { ! String text = label.getNodeValue(); ! int thisNodeXSize = Math.max(text.length() * 7, nodeSizeX); ! result.width = thisNodeXSize; ! } ! return result; } ! private Dimension drawNode(Node raiz, String number, ! Point leftTopCorner, ! Writer output) { ! Node result = searchNodeByNumber(raiz,number); ! //System.out.println("Looking for transitions"); ! Node[] transitions = search(raiz,"transition","origin",number); ! Dimension boundingBox = new Dimension(0,0); ! int childYBaseLine = leftTopCorner.y + marginY + nodeSizeY; ! Vector childBoxes = new Vector(); ! for (int i = 0; i < transitions.length ; i++) { ! String destiny = transitions[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, ! mySize(result).width); ! boundingBox.height += nodeSizeY + marginY; + // System.out.println("Aqui pinto la cajita"); + Point nodePoint = new Point((boundingBox.width - marginX)/2 + + leftTopCorner.x + , leftTopCorner.y ); ! NodeDrawData[] destinations = (NodeDrawData[])childBoxes.toArray(new NodeDrawData[childBoxes.size()]); ! for (int i = 0; i < destinations.length ; i++) { ! printTransition(nodePoint,destinations[i],output); ! } ! printNode(result,nodePoint,output); ! ! return boundingBox; } ! private void printTransition(Point origen, NodeDrawData destino, Writer output) { ! try { ! output.write("<path d=\"M " ! + origen.x // + currentNodeSizeX / 2 ! + " " + (origen.y+nodeSizeY) ! + " L " ! + (destino.origin.x + destino.size.width/2) ! + " " + destino.origin.y + "\" \n"); ! output.write("id=\"" + idGenerator.createTransitionId() ! + "\"\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"); ! } } private void printConventionalNode(Node node, Point leftTopPoint, ! Writer output) { ! try { ! int currentNodeSizeX = mySize(node).width; ! //the poing i receive is really de middle of boundingBox ! output.write("<rect \n " ! + " width=\"" + currentNodeSizeX + "\" \n" ! + " height=\"" + nodeSizeY + "\" \n" ! + " x=\"" + (leftTopPoint.x - (currentNodeSizeX / 2)) + "\" \n" ! + " y=\"" + leftTopPoint.y + "\" \n" ! + " id=\""+node.getAttributes().getNamedItem("number").getNodeValue()+"\" \n" ! + " style=\"fill:" + conventionalNodeColor ! + ";fill-opacity:1;\" " ! + " /> \n"); ! Node label = node.getAttributes().getNamedItem("label"); ! if (label != null) ! output.write("<text \n" ! + " x=\"" + (leftTopPoint.x - currentNodeSizeX/2.5) + "\" \n" ! + " y=\"" + (leftTopPoint.y + nodeSizeY/2 ) + "\" \n" ! + " id=\"" + idGenerator.createTextId() ! + "\">\n" ! + " <tspan id=\"" + idGenerator.createTextId() ! + "\" >\n " ! + label.getNodeValue() ! + "\n </tspan>\n" ! + "</text>"); ! ! } catch (IOException e) { ! System.out.println("Error writing Node"); ! } } ! private Node[] filterSubstitutionNodes(NodeList original) { ! Vector results = new Vector(); ! int elements = original.getLength(); ! for (int i = 0; i < elements; i++) { ! Node currentNode = original.item(i); ! int nodeType = currentNode.getNodeType(); ! if (nodeType == Node.ELEMENT_NODE && ! currentNode.getNodeName().equals("substitution")) ! results.add(currentNode); ! } ! return (Node [])results.toArray(new Node[results.size()]); } private void printSolutionNode(Node node, Point leftTopPoint, ! Writer output) { ! try { ! int currentNodeSizeX = mySize(node).width; ! output.write("<rect \n " ! + " width=\"" + currentNodeSizeX + "\" \n" ! + " height=\"" + nodeSizeY + "\" \n" ! + " x=\"" + (leftTopPoint.x - (currentNodeSizeX / 2)) + "\" \n" ! + " y=\"" + leftTopPoint.y + "\" \n" ! + " id=\"" +node.getAttributes().getNamedItem("number").getNodeValue()+"\" \n" ! + " style=\"fill:" + solutionNodeColor ! +";fill-opacity:1;\" " ! + " /> \n"); ! Node[] subst = filterSubstitutionNodes(node.getChildNodes()); ! int numberOfSubst = subst.length; ! ! float substYPoint = leftTopPoint.y + nodeSizeY/2; ! for (int i = 0; i < numberOfSubst; i++) { ! output.write("<text \n" ! + " x=\"" + (leftTopPoint.x - currentNodeSizeX/2.5) + "\" \n" ! + " y=\"" + (substYPoint + 12*i) + "\" \n" ! + " id=\"" + idGenerator.createTextId() ! + "\">\n" ! + " <tspan id=\"" + idGenerator.createTextId() ! + "\" >\n " ! + subst[i].getAttributes().getNamedItem("variable").getNodeValue() ! + " : " ! + subst[i].getAttributes().getNamedItem("value").getNodeValue() ! + "\n </tspan>\n" ! + "</text>"); ! } ! } catch (IOException io) { ! System.out.println("Error writing Solution node"); ! } } // And the refactoring winner is... ! private void printNode(Node node, Point leftTopPoint, ! Writer output) { ! //System.out.println("En el punto (" + leftTopPoint.x + "," ! // + leftTopPoint.y + ") imprimo el nodo " ! // + node.getNodeName()); ! // + node.getAttributes().getNamedItem("number").getNodeValue()); ! if (node.getNodeName().equals("solution")) ! printSolutionNode(node, leftTopPoint, output); ! else ! printConventionalNode(node, leftTopPoint, output); } ! /** * Aux methods ***/ private String svgInit(int height, int width) ! throws IOException { ! StringWriter w = new StringWriter(); ! 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 " ! + " height=\"" + height + "\" width=\"" + width + "\"> \n"); ! return w.toString(); } ! private String svgEnd() ! throws IOException { ! return new String("\n</svg>\n"); } *************** *** 330,375 **** * Main method ** ************************************* */ ! public String transform(String xmlString) { - - if (xmlString == null) - return null; ! 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(); ! Dimension total = drawNode(document,"1",new Point(10,10),svg); ! setSize(total); ! //System.out.println("Dimensiones totales: " + total.width + " " ! // + total.height); ! ! String result = svgInit(total.height, total.width).concat(svg.toString()).concat(svgEnd()); ! //System.out.println("Pues no paso nada malo"); ! return result; ! } ! 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; ! } //FIXME: Is necesary IOException here??? ! catch (IOException e) { ! System.out.println("I/O Error: File Not Exist or Problems with Writers"); ! return null; ! } } --- 307,349 ---- * Main method ** ************************************* */ ! public String transform(String xmlString) { ! if (xmlString == null) ! return null; ! 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(); ! ! Dimension total = drawNode(document,"1",new Point(10,10),svg); ! setSize(total); ! //System.out.println("Dimensiones totales: " + total.width + " " ! // + total.height); ! ! String result = svgInit(total.height, total.width).concat(svg.toString()).concat(svgEnd()); ! ! return result; ! } 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; ! } //FIXME: Is necesary IOException here??? ! catch (IOException e) { ! System.out.println("I/O Error: File Not Exist or Problems with Writers"); ! return null; ! } } *************** *** 382,386 **** */ public Dimension getSize() { ! return size; } --- 356,360 ---- */ public Dimension getSize() { ! return size; } *************** *** 390,397 **** */ public void setSize(Dimension newSize) { ! this.size = newSize; } ! //public static void main(String argv[]) { // SvgGenerator on = new SvgGenerator(); --- 364,371 ---- */ public void setSize(Dimension newSize) { ! this.size = newSize; } ! //public static void main(String argv[]) { // SvgGenerator on = new SvgGenerator(); |