/** * Unmarshall a Genotype instance from a given XML Element representation. Its population of * Chromosomes will be unmarshalled from the Chromosome sub-elements. * * @param a_activeConfiguration the current active Configuration object that is to be used during * construction of the Genotype and Chromosome instances * @param a_xmlElement the XML Element representation of the Genotype * @return a new Genotype instance, complete with a population of Chromosomes, setup with the data * from the XML Element representation * @throws ImproperXMLException if the given Element is improperly structured or missing data * @throws InvalidConfigurationException if the given Configuration is in an inconsistent state * @throws UnsupportedRepresentationException if the actively configured Gene implementation does * not support the string representation of the alleles used in the given XML document * @throws GeneCreationException if there is a problem creating or populating a Gene instance * @author Neil Rotstan * @author Klaus Meffert * @since 1.0 */ public static Genotype getGenotypeFromElement( Configuration a_activeConfiguration, Element a_xmlElement) throws ImproperXMLException, InvalidConfigurationException, UnsupportedRepresentationException, GeneCreationException { // Sanity check. Make sure the XML element isn't null and that it // actually represents a genotype. if (a_xmlElement == null || !(a_xmlElement.getTagName().equals(GENOTYPE_TAG))) { throw new ImproperXMLException( "Unable to build Genotype instance from XML Element: " + "given Element is not a 'genotype' element."); } // Fetch all of the nested chromosome elements and convert them // into Chromosome instances. // ------------------------------------------------------------ NodeList chromosomes = a_xmlElement.getElementsByTagName(CHROMOSOME_TAG); int numChromosomes = chromosomes.getLength(); Population population = new Population(a_activeConfiguration, numChromosomes); for (int i = 0; i < numChromosomes; i++) { population.addChromosome( getChromosomeFromElement(a_activeConfiguration, (Element) chromosomes.item(i))); } // Construct a new Genotype with the chromosomes and return it. // ------------------------------------------------------------ return new Genotype(a_activeConfiguration, population); }
/** * Unmarshall a Chromosome instance from a given XML Element representation. * * @param a_activeConfiguration the current active Configuration object that is to be used during * construction of the Chromosome * @param a_xmlElement the XML Element representation of the Chromosome * @return a new Chromosome instance setup with the data from the XML Element representation * @throws ImproperXMLException if the given Element is improperly structured or missing data * @throws InvalidConfigurationException if the given Configuration is in an inconsistent state * @throws UnsupportedRepresentationException if the actively configured Gene implementation does * not support the string representation of the alleles used in the given XML document * @throws GeneCreationException if there is a problem creating or populating a Gene instance * @author Neil Rotstan * @since 1.0 */ public static Chromosome getChromosomeFromElement( Configuration a_activeConfiguration, Element a_xmlElement) throws ImproperXMLException, InvalidConfigurationException, UnsupportedRepresentationException, GeneCreationException { // Do some sanity checking. Make sure the XML Element isn't null and // that in fact represents a chromosome. // ----------------------------------------------------------------- if (a_xmlElement == null || !(a_xmlElement.getTagName().equals(CHROMOSOME_TAG))) { throw new ImproperXMLException( "Unable to build Chromosome instance from XML Element: " + "given Element is not a 'chromosome' element."); } // Extract the nested genes element and make sure it exists. // --------------------------------------------------------- Element genesElement = (Element) a_xmlElement.getElementsByTagName(GENES_TAG).item(0); if (genesElement == null) { throw new ImproperXMLException( "Unable to build Chromosome instance from XML Element: " + "'genes' sub-element not found."); } // Construct the genes from their representations. // ----------------------------------------------- Gene[] geneAlleles = getGenesFromElement(a_activeConfiguration, genesElement); // Construct the new Chromosome with the genes and return it. // ---------------------------------------------------------- return new Chromosome(a_activeConfiguration, geneAlleles); }
public NodeList getImediateElementsByTagName(Element el, String tagname) { NodeList list = el.getElementsByTagName(tagname); SimpleNodeList result = new SimpleNodeList(); int length = list.getLength(); for (int i = 0; i < length; i++) { Node node = list.item(i); if ((Element) node.getParentNode() == el) { result.add(node); } } return result; }
public Command executeStep(Element stepRow) throws Exception { Command command = new Command(); NodeList stepFields = stepRow.getElementsByTagName("td"); String cmd = stepFields.item(0).getTextContent().trim(); command.cmd = cmd; ArrayList<String> argList = new ArrayList<String>(); if (stepFields.getLength() == 1) { // skip comments command.result = "OK"; return command; } for (int i = 1; i < stepFields.getLength(); i++) { String content = stepFields.item(i).getTextContent(); content = content.replaceAll(" +", " "); content = content.replace('\u00A0', ' '); content = content.trim(); argList.add(content); } String args[] = argList.toArray(new String[0]); command.args = args; if (this.verbose) { System.out.println(cmd + " " + Arrays.asList(args)); } try { command.result = this.commandProcessor.doCommand(cmd, args); command.error = false; } catch (Exception e) { command.result = e.getMessage(); command.error = true; } command.failure = command.error && !cmd.startsWith("verify"); if (this.verbose) { System.out.println(command.result); } return command; }
/** * 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); } } } } }
public boolean runSuite(String filename) throws Exception { if (this.verbose) { System.out.println( "Running test suite " + filename + " against " + this.host + ":" + this.port + " with " + this.browser); } TestSuite suite = new TestSuite(); long start = System.currentTimeMillis(); suite.numTestPasses = 0; suite.file = new File(filename); File suiteDirectory = suite.file.getParentFile(); this.document = parseDocument(filename); Element table = (Element) this.document.getElementsByTagName("table").item(0); NodeList tableRows = table.getElementsByTagName("tr"); Element tableNameRow = (Element) tableRows.item(0); suite.name = tableNameRow.getTextContent(); suite.result = true; suite.tests = new Test[tableRows.getLength() - 1]; for (int i = 1; i < tableRows.getLength(); i++) { Element tableRow = (Element) tableRows.item(i); Element cell = (Element) tableRow.getElementsByTagName("td").item(0); Element link = (Element) cell.getElementsByTagName("a").item(0); Test test = new Test(); test.label = link.getTextContent(); test.file = new File(suiteDirectory, link.getAttribute("href")); SeleniumHtmlClient subclient = new SeleniumHtmlClient(); subclient.setHost(this.host); subclient.setPort(this.port); subclient.setBrowser(this.browser); // subclient.setResultsWriter(this.resultsWriter); subclient.setBaseUrl(this.baseUrl); subclient.setVerbose(this.verbose); subclient.runTest(test); if (test.result) { suite.numTestPasses++; } suite.result &= test.result; suite.tests[i - 1] = test; } long end = System.currentTimeMillis(); suite.totalTime = (end - start) / 1000; if (this.resultsWriter != null) { this.resultsWriter.write("<html><head>\n"); this.resultsWriter.write("<style type='text/css'>\n"); this.resultsWriter.write( "body, table {font-family: Verdana, Arial, sans-serif;font-size: 12;}\n"); this.resultsWriter.write("table {border-collapse: collapse;border: 1px solid #ccc;}\n"); this.resultsWriter.write("th, td {padding-left: 0.3em;padding-right: 0.3em;}\n"); this.resultsWriter.write("a {text-decoration: none;}\n"); this.resultsWriter.write(".title {font-style: italic;}"); this.resultsWriter.write(".selected {background-color: #ffffcc;}\n"); this.resultsWriter.write(".status_done {background-color: #eeffee;}\n"); this.resultsWriter.write(".status_passed {background-color: #ccffcc;}\n"); this.resultsWriter.write(".status_failed {background-color: #ffcccc;}\n"); this.resultsWriter.write( ".breakpoint {background-color: #cccccc;border: 1px solid black;}\n"); this.resultsWriter.write("</style>\n"); this.resultsWriter.write("<title>" + suite.name + "</title>\n"); this.resultsWriter.write("</head><body>\n"); this.resultsWriter.write("<h1>Test suite results </h1>\n\n"); this.resultsWriter.write("<table>\n"); this.resultsWriter.write( "<tr>\n<td>result:</td>\n<td>" + (suite.result ? "passed" : "failed") + "</td>\n</tr>\n"); this.resultsWriter.write( "<tr>\n<td>totalTime:</td>\n<td>" + suite.totalTime + "</td>\n</tr>\n"); this.resultsWriter.write( "<tr>\n<td>numTestTotal:</td>\n<td>" + suite.tests.length + "</td>\n</tr>\n"); this.resultsWriter.write( "<tr>\n<td>numTestPasses:</td>\n<td>" + suite.numTestPasses + "</td>\n</tr>\n"); int numTestFailures = suite.tests.length - suite.numTestPasses; this.resultsWriter.write( "<tr>\n<td>numTestFailures:</td>\n<td>" + numTestFailures + "</td>\n</tr>\n"); this.resultsWriter.write("<tr>\n<td>numCommandPasses:</td>\n<td>0</td>\n</tr>\n"); this.resultsWriter.write("<tr>\n<td>numCommandFailures:</td>\n<td>0</td>\n</tr>\n"); this.resultsWriter.write("<tr>\n<td>numCommandErrors:</td>\n<td>0</td>\n</tr>\n"); this.resultsWriter.write("<tr>\n<td>Selenium Version:</td>\n<td>2.24</td>\n</tr>\n"); this.resultsWriter.write("<tr>\n<td>Selenium Revision:</td>\n<td>.1</td>\n</tr>\n"); // test suite this.resultsWriter.write("<tr>\n<td>\n"); this.resultsWriter.write( "<table id=\"suiteTable\" class=\"selenium\" border=\"1\" cellpadding=\"1\" cellspacing=\"1\"><tbody>\n"); this.resultsWriter.write( "<tr class=\"title " + (suite.result ? "status_passed" : "status_failed") + "\"><td><b>Test Suite</b></td></tr>\n"); int i = 0; for (Test test : suite.tests) { this.resultsWriter.write( "<tr class=\"" + (test.result ? "status_passed" : "status_failed") + "\"><td><a href=\"#testresult" + i + "\">" + test.name + "</a></td></tr>"); i++; } this.resultsWriter.write( "</tbody></table>\n</td>\n<td> </td>\n</tr>\n</table>\n<table>"); int j = 0; for (Test test : suite.tests) { this.resultsWriter.write( "<tr><td><a name=\"testresult" + j + "\">" + test.file + "</a><br/><div>\n"); this.resultsWriter.write("<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\">\n"); this.resultsWriter.write( "<thead>\n<tr class=\"title " + (test.result ? "status_passed" : "status_failed") + "\"><td rowspan=\"1\" colspan=\"3\">" + test.name + "</td></tr>"); this.resultsWriter.write("</thead><tbody>\n"); for (Command command : test.commands) { boolean result = command.result.startsWith("OK"); boolean isAssert = command.cmd.startsWith("assert") || command.cmd.startsWith("verify"); ; if (!isAssert) { this.resultsWriter.write( "<tr class=\"" + (result ? "status_done" : "") + "\">\n<td>\n"); } else { this.resultsWriter.write( "<tr class=\"" + (result ? "status_passed" : "status_failed") + "\">\n<td>\n"); } this.resultsWriter.write(command.cmd); this.resultsWriter.write("</td>\n"); if (command.args != null) { for (String arg : Arrays.asList(command.args)) { this.resultsWriter.write("<td>" + arg + "</td>\n"); } } } this.resultsWriter.write("</tr>\n"); this.resultsWriter.write("</tbody></table>\n"); this.resultsWriter.write("</div></td>\n<td> </td>\n</tr>"); j++; } int k = 0; for (Test test : suite.tests) { k++; } this.resultsWriter.write("</tbody></table>\n</td><td> </td>\n</tr>\n</table>\n"); this.resultsWriter.write("</body></html>"); } return suite.result; }
/** * Unmarshall a Chromosome instance from a given XML Element representation. * * @param a_activeConfiguration current Configuration object * @param a_xmlElement the XML Element representation of the Chromosome * @return a new Chromosome instance setup with the data from the XML Element representation * @throws ImproperXMLException if the given Element is improperly structured or missing data * @throws UnsupportedRepresentationException if the actively configured Gene implementation does * not support the string representation of the alleles used in the given XML document * @throws GeneCreationException if there is a problem creating or populating a Gene instance * @author Neil Rotstan * @since 1.0 */ public static Gene[] getGenesFromElement( Configuration a_activeConfiguration, Element a_xmlElement) throws ImproperXMLException, UnsupportedRepresentationException, GeneCreationException { // Do some sanity checking. Make sure the XML Element isn't null and // that it in fact represents a set of genes. // ----------------------------------------------------------------- if (a_xmlElement == null || !(a_xmlElement.getTagName().equals(GENES_TAG))) { throw new ImproperXMLException( "Unable to build Chromosome instance from XML Element: " + "given Element is not a 'genes' element."); } List genes = Collections.synchronizedList(new ArrayList()); // Extract the nested gene elements. // --------------------------------- NodeList geneElements = a_xmlElement.getElementsByTagName(GENE_TAG); if (geneElements == null) { throw new ImproperXMLException( "Unable to build Gene instances from XML Element: " + "'" + GENE_TAG + "'" + " sub-elements not found."); } // For each gene, get the class attribute so we know what class // to instantiate to represent the gene instance, and then find // the child text node, which is where the string representation // of the allele is located, and extract the representation. // ------------------------------------------------------------- int numberOfGeneNodes = geneElements.getLength(); for (int i = 0; i < numberOfGeneNodes; i++) { Element thisGeneElement = (Element) geneElements.item(i); thisGeneElement.normalize(); // Fetch the class attribute and create an instance of that // class to represent the current gene. // -------------------------------------------------------- String geneClassName = thisGeneElement.getAttribute(CLASS_ATTRIBUTE); Gene thisGeneObject; Class geneClass = null; try { geneClass = Class.forName(geneClassName); try { Constructor constr = geneClass.getConstructor(new Class[] {Configuration.class}); thisGeneObject = (Gene) constr.newInstance(new Object[] {a_activeConfiguration}); } catch (NoSuchMethodException nsme) { // Try it by calling method newGeneInternal. // ----------------------------------------- Constructor constr = geneClass.getConstructor(new Class[] {}); thisGeneObject = (Gene) constr.newInstance(new Object[] {}); thisGeneObject = (Gene) PrivateAccessor.invoke( thisGeneObject, "newGeneInternal", new Class[] {}, new Object[] {}); } } catch (Throwable e) { throw new GeneCreationException(geneClass, e); } // Find the text node and fetch the string representation of // the allele. // --------------------------------------------------------- NodeList children = thisGeneElement.getChildNodes(); int childrenSize = children.getLength(); String alleleRepresentation = null; for (int j = 0; j < childrenSize; j++) { Element alleleElem = (Element) children.item(j); if (alleleElem.getTagName().equals(ALLELE_TAG)) { alleleRepresentation = alleleElem.getAttribute("value"); } if (children.item(j).getNodeType() == Node.TEXT_NODE) { // We found the text node. Extract the representation. // --------------------------------------------------- alleleRepresentation = children.item(j).getNodeValue(); break; } } // Sanity check: Make sure the representation isn't null. // ------------------------------------------------------ if (alleleRepresentation == null) { throw new ImproperXMLException( "Unable to build Gene instance from XML Element: " + "value (allele) is missing representation."); } // Now set the value of the gene to that reflect the // string representation. // ------------------------------------------------- try { thisGeneObject.setValueFromPersistentRepresentation(alleleRepresentation); } catch (UnsupportedOperationException e) { throw new GeneCreationException( "Unable to build Gene because it does not support the " + "setValueFromPersistentRepresentation() method."); } // Finally, add the current gene object to the list of genes. // ---------------------------------------------------------- genes.add(thisGeneObject); } return (Gene[]) genes.toArray(new Gene[genes.size()]); }
private Element getElement(Element root, String name) { NodeList list = root.getElementsByTagName(name); Element el = (Element) list.item(0); return el; }
public ShowFileData loadShow(String s) { Vector<Pair> links = new Vector<Pair>(); /* ShowFileData show = new ShowFileData(); show.setSettings(new CameoSettings()); show.setPatch(new CameoPatch(0,0)); show.setCueList(new CueList(0)); show.setMagicSheet(new MagicSheet()); */ SettingsData settingsData = new SettingsData(); MagicSheetData magicSheetData = new MagicSheetData(); CueListData cueListData = new CueListData(); PatchData patchData = new PatchData(); Document doc; try { doc = _builder.parse(s); Element root = doc.getDocumentElement(); Element settings = getElement(root, _settings); Element patch = getElement(root, _patch); Element cues = getElement(root, _cues); Element magic = getElement(root, _magicSheet); // TODO test that all of these are non null and report incorrect file missing ... String recordMode = getElement(settings, _recordMode).getTextContent(); settingsData.put(_recordMode, recordMode); /* if(recordMode.equals(RecordMode.TRACKING.toString())) show._settings._mode = RecordMode.TRACKING; else if(recordMode.equals(RecordMode.CUE_ONLY.toString())) show._settings._mode = RecordMode.CUE_ONLY; */ settingsData.put(_totalChannels, getElement(settings, _totalChannels).getTextContent()); settingsData.put(_totalDimmers, getElement(settings, _totalDimmers).getTextContent()); settingsData.put(_dUpTime, getElement(settings, _dUpTime).getTextContent()); settingsData.put(_dDownTime, getElement(settings, _dDownTime).getTextContent()); settingsData.put(_gotoCueTime, getElement(settings, _gotoCueTime).getTextContent()); settingsData.put(_title, getElement(settings, _title).getTextContent()); settingsData.put(_comment, getElement(settings, _comment).getTextContent()); settingsData.put(_channelPerLine, getElement(settings, _channelPerLine).getTextContent()); settingsData.put(_channelHGroup, getElement(settings, _channelHGroup).getTextContent()); settingsData.put(_channelVGroup, getElement(settings, _channelVGroup).getTextContent()); /* show._settings._totalChannels = Integer.parseInt(getElement(settings, _totalChannels).getTextContent()); show._settings._totalDimmers = Integer.parseInt(getElement(settings, _totalDimmers).getTextContent()); show._settings._upTime = Integer.parseInt(getElement(settings, _dUpTime).getTextContent()); show._settings._downTime = Integer.parseInt(getElement(settings, _dDownTime).getTextContent()); show._settings._gotoCueTime = Integer.parseInt(getElement(settings, _gotoCueTime).getTextContent()); show._settings._showTitle = getElement(settings, _title).getTextContent(); show._settings._showComment = getElement(settings, _comment).getTextContent(); show._settings._ChannelsPerLine = Integer.parseInt(getElement(settings, _channelPerLine).getTextContent()); show._settings._ChannelGrouping = Integer.parseInt(getElement(settings, _channelHGroup).getTextContent()); show._settings._LineGrouping = Integer.parseInt(getElement(settings, _channelVGroup).getTextContent()); */ int totalChannels = Integer.parseInt(settingsData.get(_totalChannels)); patchData.setTotalChannels(totalChannels); patchData.setTotalDimmers(Integer.parseInt(settingsData.get(_totalDimmers))); cueListData.setTotalChannels(totalChannels); // show.setPatch(new CameoPatch(show._settings._totalChannels, show._settings._totalDimmers)); // show.setCueList(new CueList(show._settings._totalChannels)); NodeList list = patch.getElementsByTagName(_channel); for (int x = 0; x < list.getLength(); x++) { Element el = (Element) list.item(x); int number = Integer.parseInt(el.getAttribute(_number)); patchDims(patchData, number, el.getTextContent()); } list = cues.getElementsByTagName(_cue); for (int x = 0; x < list.getLength(); x++) { Element el = (Element) list.item(x); CueData cue = new CueData(); cue.setDescription(el.getAttribute(_discription)); NodeList list2 = el.getElementsByTagName(_channel); for (int y = 0; y < list2.getLength(); y++) { Element chan = (Element) list2.item(y); setLevel(cue, chan.getAttribute(_number), chan.getTextContent()); } int number = Integer.parseInt(el.getAttribute(_number)); int upTime = Integer.parseInt(el.getAttribute(_uptime)); int downTime = Integer.parseInt(el.getAttribute(_downTime)); int delayUpTime = Integer.parseInt(el.getAttribute(_upTimeDelay)); int delayDownTime = Integer.parseInt(el.getAttribute(_downTimeDelay)); FadeData fade = new FadeData(number, upTime, downTime, delayUpTime, delayDownTime, cue); if (el.hasAttribute(_followtime)) fade.setFollowTime(Integer.parseInt(el.getAttribute(_followtime))); if (el.hasAttribute(_nextCue)) fade.setNextCue(Integer.parseInt(el.getAttribute(_nextCue))); cueListData.add(fade); } // TODO this needs to be done in the extractor // for(Pair p : links) // show._cuelist.getCueNumbered(p._first).setNextCue(show._cuelist.getCueNumbered(p._second)); list = magic.getElementsByTagName(_channel); for (int x = 0; x < list.getLength(); x++) { Element el = (Element) list.item(x); // Element xval = getElement(el, _x); // Element yval = getElement(el, _y); Integer number = new Integer(Integer.parseInt(el.getAttribute(_number)) - 1); double xpos = Double.parseDouble(el.getAttribute(_x)); double ypos = Double.parseDouble(el.getAttribute(_y)); magicSheetData.put(number, new MagicChannelData(xpos, ypos)); } } catch (DOMException e) { System.err.println(e); } catch (SAXException e) { System.err.println(e); } catch (FileNotFoundException e) { JOptionPane.showMessageDialog( new JFrame(""), "The show file " + s + " was not found, loading default show."); } catch (IOException e) { System.err.println(e); } return new ShowFileData(settingsData, cueListData, patchData, magicSheetData); }