private void addCheckBox( Element form, boolean flag, String prompt, String paramName, String onclick) { Element check = new Element(HTMLNames.INPUT); check.addAttribute(HTMLNames.TYPE, HTMLNames.CHECKBOX); check.addAttribute(HTMLNames.NAME, paramName); if (flag) check.addAttribute(HTMLNames.CHECKED, null); if (onclick != null) check.addAttribute(HTMLNames.ONCLICK, onclick); Element span = new Element(HTMLNames.SPAN); span.addText(prompt); form.addChild(span); form.addChild(check); }
protected void open(String name) { Element child = new Element(name, current); Element parent = current; if (parent != null) parent.addChild(child); elements.add(child); current = child; }
private static Element parseElement(XMLStreamReader xsr) throws XMLStreamException { // xsr points to a START_ELEMENT event. Create the element and read all its attributes // Then read all its children events Element element = new Element(xsr.getLocalName()); // text that will be added to the element. Text can come in different events, so we add it here // and add it to the element at the end StringBuilder elementText = new StringBuilder(); int attributeCount = xsr.getAttributeCount(); for (int i = 0; i < attributeCount; i++) { element.putAttribute(xsr.getAttributeLocalName(i), xsr.getAttributeValue(i)); } while (xsr.hasNext()) { xsr.next(); if (xsr.getEventType() == XMLStreamConstants.END_ELEMENT) { // element is closed. Move the cursor and return it // check if there is some text to add before (empty text is not added, but added text is not // trimmed) // we set empty text also if the element has no children if (!elementText.toString().trim().isEmpty() || !element.hasChildren()) { element.setText(elementText.toString()); } // xsr.next(); return element; } else if (xsr.getEventType() == XMLStreamConstants.CHARACTERS) { // an attribute of the current element elementText.append(xsr.getText()); } else if (xsr.getEventType() == XMLStreamConstants.START_ELEMENT) { // new element begins -> read it recursively and add it to the current element element.addChild(parseElement(xsr)); } } // we reached the end of the document without the tag end -> error parsing throw new XMLStreamException( "End of the document unexpectedly reached. Element " + element.getName() + " not closed"); }
/** * Add a dropdown menu of lengths * * @param prompt the prompt explaining the menu * @param parent the parnet element to attach it to */ private void addLengthSelector(String prompt, Element parent) { Element select = new Element(HTMLNames.SELECT); select.addAttribute(HTMLNames.NAME, Params.LENGTH); addOption(select, "100", length == 100); addOption(select, "150", length == 150); addOption(select, "200", length == 200); addOption(select, "250", length == 250); parent.addText(prompt); parent.addChild(select); }
/** * Add a dropdown menu containing all the nested versions and groups * * @param parent the parent element to attach it to * @throws Exception */ void addVersionDropdown(Element parent) throws Exception { Element select = new Element(HTMLNames.SELECT); String versions = getVersions(); String[] opts = versions.split(","); // remove trailing LF- HashSet<String> selected = new HashSet<String>(); if (selectedVersions != null) { String[] shortNames = selectedVersions.split(","); for (int i = 0; i < shortNames.length; i++) selected.add(shortNames[i]); } opts[opts.length - 1] = opts[opts.length - 1].trim(); for (int i = 0; i < opts.length; i++) { String[] cols = opts[i].split("/"); if (cols.length > 0 && cols[0].length() == 0) cols = popArray(cols); if (cols.length > 1) { HTMLOptGroup group = getGroup(select, cols[0]); if (selected.contains(opts[i])) cols[cols.length - 1] += " ×"; if (group == null) { group = new HTMLOptGroup(cols[0]); select.addChild(group); } group.add(opts[i], popArray(cols)); } else if (cols.length == 1) { Element option = new Element(HTMLNames.OPTION); select.addChild(option); String content = cols[cols.length - 1]; if (selected.contains(opts[i])) content += " \327"; option.addText(content); option.addAttribute(HTMLNames.VALUE, opts[i]); } } parent.addChild(select); if (!someVersions) select.addAttribute(HTMLNames.DISABLED, null); select.addAttribute(HTMLNames.ONCHANGE, "checkmark(this)"); select.addAttribute(HTMLNames.ID, "selector"); Element hidden = new Element(HTMLNames.INPUT); hidden.addAttribute(HTMLNames.TYPE, HTMLNames.HIDDEN); hidden.addAttribute(HTMLNames.ID, "versions"); hidden.addAttribute(HTMLNames.NAME, Params.SELECTED_VERSIONS); parent.addChild(hidden); }
/** * Add a dropdown menu of lengths * * @param prompt the prompt explaining the menu * @param parent the parent element to attach it to */ private void addOffsetSelector(String prompt, Element parent) { Element select = new Element(HTMLNames.SELECT); select.addAttribute(HTMLNames.NAME, Params.OFFSET); addOption(select, "0", offset == 0); addOption(select, "100", offset == 100); addOption(select, "200", offset == 200); addOption(select, "300", offset == 300); addOption(select, "400", offset == 400); addOption(select, "500", offset == 500); addOption(select, "600", offset == 600); addOption(select, "700", offset == 700); parent.addText(prompt); parent.addChild(select); }
// public static void write(OutputStream stream, Element element, int hashLength) throws // XMLStreamException { // XMLOutputFactory xof = XMLOutputFactory.newInstance(); // IndentingXMLStreamWriter xtw = new // IndentingXMLStreamWriter(xof.createXMLStreamWriter(stream)); // writeXMLStreamWriter(xtw, element, hashLength); // } // private static void writeXMLStreamWriter(XMLStreamWriter xtw, Element element, int hashLength) throws XMLStreamException { if (hashLength > 0) { String hash = element.getHash(hashLength); Element hashElement = new Element(CRC_ELEMENT); hashElement.setText(hash); element.addChild(hashElement); } writeElement(xtw, element); xtw.writeEndDocument(); xtw.flush(); xtw.close(); if (hashLength > 0) { element.removeChildren(CRC_ELEMENT); } }
public void testXmlSerializer() throws Exception { Format root = new DataFormat("root"); Format person = root.addChild("Person", Format.Form.STRUCT); person.setProperty("test1", new Value("prop1")); person.setProperty("test2", new Value(119)); person.setProperty("test3", new Value(true)); person.setProperty("test4", new Value(3.1415926f)); person.addChild("name", Format.Form.FIELD, Type.STRING); Format asset = person.addChild("asset", Format.Form.ARRAYOFSTRUCT); asset.addChild("test", Format.Form.ARRAYOFFIELD, Type.STRING); asset.addChild("name", Format.Form.FIELD, Type.STRING); asset.addChild("price", Format.Form.FIELD, Type.FLOAT); Format name = asset .addChild("vendor", Format.Form.STRUCT) .addChild("name", Format.Form.FIELD, Type.STRING); Element personData = new DataElement(person); Element nameData = personData.addChild("name"); nameData.setValue("James wang"); assertEquals("James wang", nameData.getValue().getString()); Element assetData = personData.addChild("asset"); Element assetDataItem = assetData.addArrayItem(); assetDataItem.addChild("test").addArrayItem().setValue("test collection default index"); assetDataItem.addChild("name").setValue("Mac air book"); assetDataItem.addChild("price").setValue(12000.05f); assetDataItem.addChild("vendor").addChild("name").setValue("Apple"); assetDataItem = assetData.addArrayItem(); assetDataItem.addChild("name").setValue("HP computer"); assetDataItem.addChild("price").setValue(15000.05f); assetDataItem.addChild("vendor").addChild("name").setValue("HP"); ElementXmlSerializer serializer = new ElementXmlSerializer(personData, true); ByteArrayOutputStream baos = new ByteArrayOutputStream(); serializer.write(baos); System.out.println(baos.toString()); }
/** * Get the content of this test: a table of versions * * @return a select element object with appropriate attributes and children */ @Override public Element getContent() { try { Element div = new Element("div"); String rawURL = "http://localhost:8080" + Service.PREFIX + "/html/table"; String urn = Utils.escape(docID); rawURL = calliope.URLEncoder.append(rawURL, urn); // add required params rawURL = addGetParam(rawURL, Params.HIDE_MERGED, (hideMerged) ? "1" : "0"); rawURL = addGetParam(rawURL, Params.COMPACT, (compact) ? "1" : "0"); rawURL = addGetParam(rawURL, Params.WHOLE_WORDS, (wholeWords) ? "1" : "0"); rawURL = addGetParam(rawURL, Params.LENGTH, Integer.toString(length)); rawURL = addGetParam(rawURL, Params.OFFSET, Integer.toString(offset)); rawURL = addGetParam(rawURL, Params.SOME_VERSIONS, (someVersions) ? "1" : "0"); if (someVersions && selectedVersions != null) rawURL = addGetParam(rawURL, Params.SELECTED_VERSIONS, selectedVersions); URL url = new URL(rawURL); URLConnection conn = url.openConnection(); InputStream is = conn.getInputStream(); StringBuilder sb = new StringBuilder(); while (is.available() != 0) { byte[] data = new byte[is.available()]; is.read(data); sb.append(new String(data, "UTF-8")); } div.addChild(new HTMLLiteral(sb.toString())); Element panel = new Element(HTMLNames.DIV); Element form = formElement(Service.PREFIX + "/tests/table/"); Element p1 = new Element(HTMLNames.P); form.addChild(p1); addCheckBox(p1, hideMerged, "hide merged", Params.HIDE_MERGED, null); addCheckBox(p1, wholeWords, " whole words", Params.WHOLE_WORDS, null); addCheckBox(p1, compact, " compact", Params.COMPACT, null); // submit button Element submit = new Element(HTMLNames.INPUT); submit.addAttribute(HTMLNames.TYPE, HTMLNames.SUBMIT); submit.addAttribute(HTMLNames.ONCLICK, "presubmit()"); p1.addChild(submit); // next row of buttons Element p2 = new Element(HTMLNames.P); form.addChild(p2); // length and offset addLengthSelector("length:", p2); addOffsetSelector(" start offset:", p2); // all versions addCheckBox( p2, someVersions, " some versions", Params.SOME_VERSIONS, "toggleVersionSelector(this)"); addVersionDropdown(p2); panel.addChild(form); div.addChild(panel); Element explanation = new Element(HTMLNames.DIV); Element p3 = new HTMLLiteral(EXPLANATION); explanation.addChild(p3); div.addChild(explanation); return div; } catch (Exception e) { return new Text("Failed Table (HTML) test: " + e.getMessage()); } }
/** * Add the specified option to the selector * * @param select the selector element * @param value the new value */ private void addOption(Element select, String value, boolean selectit) { Element option = new Element(HTMLNames.OPTION); option.addText(value); if (selectit) option.addAttribute(HTMLNames.SELECTED, HTMLNames.SELECTED); select.addChild(option); }