/** * Converts any fields of the specified type found in the descendants of the node into static * text. * * @param compositeNode The node in which all descendants of the specified FieldType will be * converted to static text. * @param targetFieldType The FieldType of the field to convert to static text. */ public static void convertFieldsToStaticText(CompositeNode compositeNode, int targetFieldType) throws Exception { String originalNodeText = compositeNode.toString(SaveFormat.TEXT); // ExSkip FieldsHelper helper = new FieldsHelper(targetFieldType); compositeNode.accept(helper); assert (originalNodeText.equals(compositeNode.toString(SaveFormat.TEXT))) : "Error: Text of the node converted differs from the original"; // ExSkip for (Node node : (Iterable<Node>) compositeNode.getChildNodes(NodeType.ANY, true)) // ExSkip assert (!(node instanceof FieldChar && ((FieldChar) node).getFieldType() == targetFieldType)) : "Error: A field node that should be removed still remains."; // ExSkip }
/** * Reload the composite nodes of the circuit, this is recursive * * @param g the graphics that will paint the node * @throws CircuitLoadingException if the internal circuit can not be loaded */ public void reloadCompositeNodes(Graphics g) throws CircuitLoadingException { for (iterNodes = this.nodes.iterator(); iterNodes.hasNext(); ) { Node n = iterNodes.next(); if (n.getCategoryID() == Node.COMPOSITE) ((CompositeNode) n).reload(g); } }
@Test public void testCompositeFormatting() throws Exception { { Parser p = new Parser("hello%5(XYZ)"); Node t = p.parse(); Node witness = new Node(Node.LITERAL, "hello"); CompositeNode composite = new CompositeNode(); composite.setFormatInfo(new FormatInfo(5, Integer.MAX_VALUE)); Node child = new Node(Node.LITERAL, "XYZ"); composite.setChildNode(child); witness.next = composite; assertEquals(witness, t); } }
@Test public void testNested() throws Exception { { Parser p = new Parser("%top %(%child%(%h))"); Node t = p.parse(); Node witness = new KeywordNode("top"); Node w = witness.next = new Node(Node.LITERAL, " "); CompositeNode composite = new CompositeNode(); w = w.next = composite; Node child = new KeywordNode("child"); composite.setChildNode(child); composite = new CompositeNode(); child.next = composite; composite.setChildNode(new KeywordNode("h")); assertEquals(witness, t); } }
@Test public void testComposite() throws Exception { { Parser p = new Parser("hello%(%child)"); Node t = p.parse(); Node witness = new Node(Node.LITERAL, "hello"); CompositeNode composite = new CompositeNode(); Node child = new KeywordNode("child"); composite.setChildNode(child); witness.next = composite; // System.out.println("w:" + witness); // System.out.println(t); assertEquals(witness, t); } // System.out.println("testRecursive part 2"); { Parser p = new Parser("hello%(%child )"); Node t = p.parse(); Node witness = new Node(Node.LITERAL, "hello"); CompositeNode composite = new CompositeNode(); Node child = new KeywordNode("child"); composite.setChildNode(child); witness.next = composite; child.next = new Node(Node.LITERAL, " "); assertEquals(witness, t); } { Parser p = new Parser("hello%(%child %h)"); Node t = p.parse(); Node witness = new Node(Node.LITERAL, "hello"); CompositeNode composite = new CompositeNode(); Node child = new KeywordNode("child"); composite.setChildNode(child); child.next = new Node(Node.LITERAL, " "); child.next.next = new KeywordNode("h"); witness.next = composite; assertEquals(witness, t); } { Parser p = new Parser("hello%(%child %h) %m"); Node t = p.parse(); Node witness = new Node(Node.LITERAL, "hello"); CompositeNode composite = new CompositeNode(); Node child = new KeywordNode("child"); composite.setChildNode(child); child.next = new Node(Node.LITERAL, " "); child.next.next = new KeywordNode("h"); witness.next = composite; composite.next = new Node(Node.LITERAL, " "); composite.next.next = new KeywordNode("m"); assertEquals(witness, t); } { Parser p = new Parser("hello%( %child \\(%h\\) ) %m"); Node t = p.parse(); Node witness = new Node(Node.LITERAL, "hello"); CompositeNode composite = new CompositeNode(); Node child = new Node(Node.LITERAL, " "); composite.setChildNode(child); Node c = child; c = c.next = new KeywordNode("child"); c = c.next = new Node(Node.LITERAL, " ("); c = c.next = new KeywordNode("h"); c = c.next = new Node(Node.LITERAL, ") "); witness.next = composite; composite.next = new Node(Node.LITERAL, " "); composite.next.next = new KeywordNode("m"); assertEquals(witness, t); } }
/** * XML Circuit constructor * * @param file the file that contains the XML description of the circuit * @param g the graphics that will paint the node * @throws CircuitLoadingException if the internal circuit can not be loaded */ public CircuitUI(File file, Graphics g) throws CircuitLoadingException { this(""); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; Document doc; Element root; Hashtable<Integer, Link> linkstable = new Hashtable<Integer, Link>(); try { builder = factory.newDocumentBuilder(); doc = builder.parse(file); } catch (SAXException sxe) { throw new CircuitLoadingException("SAX exception raised, invalid XML file."); } catch (ParserConfigurationException pce) { throw new CircuitLoadingException( "Parser exception raised, parser configuration is invalid."); } catch (IOException ioe) { throw new CircuitLoadingException("I/O exception, file cannot be loaded."); } root = (Element) doc.getElementsByTagName("Circuit").item(0); this.setName(root.getAttribute("name")); NodeList nl = root.getElementsByTagName("Node"); Element e; Node n; Class cl; for (int i = 0; i < nl.getLength(); ++i) { e = (Element) nl.item(i); try { cl = Class.forName(e.getAttribute("class")); } catch (Exception exc) { System.err.println(exc.getMessage()); throw new RuntimeException("Circuit creation from xml."); } try { n = ((Node) cl.newInstance()); } catch (Exception exc) { System.err.println(exc.getMessage()); throw new RuntimeException("Circuit creation from xml."); } this.nodes.add(n); n.setLocation(new Integer(e.getAttribute("x")), new Integer(e.getAttribute("y"))); if (n instanceof giraffe.ui.Nameable) ((Nameable) n).setNodeName(e.getAttribute("node_name")); if (n instanceof giraffe.ui.CompositeNode) { try { ((CompositeNode) n) .load(new File(file.getParent() + "/" + e.getAttribute("file_name")), g); } catch (Exception exc) { /* try to load from the lib */ ((CompositeNode) n) .load(new File(giraffe.Giraffe.PATH + "/lib/" + e.getAttribute("file_name")), g); } } NodeList nlist = e.getElementsByTagName("Anchor"); Element el; for (int j = 0; j < nlist.getLength(); ++j) { el = (Element) nlist.item(j); Anchor a = n.getAnchor(new Integer(el.getAttribute("id"))); NodeList linklist = el.getElementsByTagName("Link"); Element link; Link l; for (int k = 0; k < linklist.getLength(); ++k) { link = (Element) linklist.item(k); int id = new Integer(link.getAttribute("id")); int index = new Integer(link.getAttribute("index")); if (id >= this.linkID) linkID = id + 1; if (linkstable.containsKey(id)) { l = linkstable.get(id); l.addLinkedAnchorAt(a, index); a.addLink(l); } else { l = new Link(id); l.addLinkedAnchorAt(a, index); this.links.add(l); linkstable.put(id, l); a.addLink(l); } } } } }