List of usage examples for org.jdom2 Element getChild
public Element getChild(final String cname, final Namespace ns)
From source file:Question.java
License:Apache License
/** * Add the content of the question to the template. This adds a string to one particular element of the XML * template.//w ww . j a va 2 s. co m * * @param questionContent We only allow the question content element of the question to vary dynamically. * @throws StoryException */ public void addQuestionContent(String questionContent) throws StoryException { // This should REALLY be done using Xpath, but Amazon's mturk doesn't use a prefix to their xmlns definition // and XPath maps this into the null namespace. logger.debug("getting the XML element for the QuestionContent..."); Element question = root.getChild("Question", ns); if (question == null) { throw new StoryException("The XML template file doesn't contain a proper Question element"); } Element qc = question.getChild("QuestionContent", ns); if (qc == null) { throw new StoryException("The XML template file doesn't contain a proper QuestionContent element"); } Element txt = qc.getChild("Text", ns); if (txt == null) { throw new StoryException("The XML template file's QuestionContent element doesn't have a Text element"); } logger.debug("adding the StoryText to the template XML..."); txt.addContent(questionContent); }
From source file:Question.java
License:Apache License
/** * This adds one radio button selection to a radio button based HIT template. If the questionTemplateFile doesn't * contain a StyleSuggestion element containing "radiobutton", then this method won't work. * // w ww .ja va2 s .co m * @param radioButtonText * @param radioButtonIdentifier * @throws StoryException */ public void addRadioButtonSpecification(String radioButtonText, String radioButtonIdentifier) throws StoryException { // This should REALLY be done using Xpath, but Amazon's mturk doesn't use a prefix to their xmlns definition // and XPath maps this into the null namespace. logger.debug("getting the XML element for the Selection..."); Element question = root.getChild("Question", null); if (question == null) { throw new StoryException("The XML template file doesn't contain a proper Question element"); } Element answerSpecification = question.getChild("AnswerSpecification", null); if (answerSpecification == null) { throw new StoryException("The XML template file doesn't contain a proper AnswerSpecification element"); } Element selectionAnswer = answerSpecification.getChild("SelectionAnswer", null); if (selectionAnswer == null) { throw new StoryException("The XML template file doesn't contain a proper SelectionAnswer element"); } Element selections = selectionAnswer.getChild("Selections", null); if (selections == null) { throw new StoryException("The XML template file's QuestionContent element doesn't have a Text element"); } // Adding in the namespace URI is the only way to keep JDOM2 from adding a null namespace to the element Element selection = new Element("Selection", "http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2005-10-01/QuestionForm.xsd"); Element selectionIdentifier = new Element("SelectionIdentifier", "http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2005-10-01/QuestionForm.xsd"); Element text = new Element("Text", "http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2005-10-01/QuestionForm.xsd"); text.addContent(radioButtonText); selectionIdentifier.addContent(radioButtonIdentifier); selection.addContent(selectionIdentifier); selection.addContent(text); selections.addContent(selection); }
From source file:ca.nrc.cadc.caom2.xml.ObservationReader.java
License:Open Source License
protected Position getPosition(Element parent, Namespace namespace, ReadContext rc) throws ObservationParsingException { Element element = getChildElement("position", parent, namespace, false); if (element == null) { return null; }/* w ww . j av a2 s .c o m*/ Position pos = new Position(); Element cur = getChildElement("bounds", element, namespace, false); if (cur != null) { if (rc.docVersion < 23) { throw new UnsupportedOperationException( "cannot convert version " + rc.docVersion + " polygon to current version"); } Attribute type = cur.getAttribute("type", xsiNamespace); String tval = type.getValue(); String circleType = namespace.getPrefix() + ":" + Circle.class.getSimpleName(); String polyType = namespace.getPrefix() + ":" + Polygon.class.getSimpleName(); if (polyType.equals(tval)) { List<Point> points = new ArrayList<Point>(); Element pes = cur.getChild("points", namespace); for (Element pe : pes.getChildren()) { // only vertex double cval1 = getChildTextAsDouble("cval1", pe, namespace, true); double cval2 = getChildTextAsDouble("cval2", pe, namespace, true); points.add(new Point(cval1, cval2)); } Element se = cur.getChild("samples", namespace); MultiPolygon poly = new MultiPolygon(); Element ves = se.getChild("vertices", namespace); for (Element ve : ves.getChildren()) { // only vertex double cval1 = getChildTextAsDouble("cval1", ve, namespace, true); double cval2 = getChildTextAsDouble("cval2", ve, namespace, true); int sv = getChildTextAsInteger("type", ve, namespace, true); poly.getVertices().add(new Vertex(cval1, cval2, SegmentType.toValue(sv))); } pos.bounds = new Polygon(points, poly); } else if (circleType.equals(tval)) { Element ce = cur.getChild("center", namespace); double cval1 = getChildTextAsDouble("cval1", ce, namespace, true); double cval2 = getChildTextAsDouble("cval2", ce, namespace, true); Point c = new Point(cval1, cval2); double r = getChildTextAsDouble("radius", cur, namespace, true); pos.bounds = new Circle(c, r); } else { throw new UnsupportedOperationException("unsupported shape: " + tval); } } cur = getChildElement("dimension", element, namespace, false); if (cur != null) { // Attribute type = cur.getAttribute("type", xsiNamespace); // String tval = type.getValue(); // String extype = namespace.getPrefix() + ":" + // Dimension2D.class.getSimpleName(); // if ( extype.equals(tval) ) // { long naxis1 = getChildTextAsLong("naxis1", cur, namespace, true); long naxis2 = getChildTextAsLong("naxis2", cur, namespace, true); pos.dimension = new Dimension2D(naxis1, naxis2); // } // else // throw new ObservationParsingException("unsupported dimension // type: " + tval); } pos.resolution = getChildTextAsDouble("resolution", element, namespace, false); pos.sampleSize = getChildTextAsDouble("sampleSize", element, namespace, false); pos.timeDependent = getChildTextAsBoolean("timeDependent", element, namespace, false); return pos; }
From source file:ca.nrc.cadc.caom2.xml.ObservationReader.java
License:Open Source License
protected Energy getEnergy(Element parent, Namespace namespace, ReadContext rc) throws ObservationParsingException { Element element = getChildElement("energy", parent, namespace, false); if (element == null) { return null; }//from ww w .j a v a 2s . com Energy nrg = new Energy(); Element cur = getChildElement("bounds", element, namespace, false); if (cur != null) { double lb = getChildTextAsDouble("lower", cur, namespace, true); double ub = getChildTextAsDouble("upper", cur, namespace, true); nrg.bounds = new Interval(lb, ub); addSamples(nrg.bounds, cur.getChild("samples", namespace), namespace, rc); } cur = getChildElement("dimension", element, namespace, false); if (cur != null) { nrg.dimension = getChildTextAsLong("dimension", element, namespace, true); } nrg.resolvingPower = getChildTextAsDouble("resolvingPower", element, namespace, false); nrg.sampleSize = getChildTextAsDouble("sampleSize", element, namespace, false); nrg.bandpassName = getChildText("bandpassName", element, namespace, false); String emb = getChildText("emBand", element, namespace, false); if (emb != null) { nrg.emBand = EnergyBand.toValue(emb); } nrg.restwav = getChildTextAsDouble("restwav", element, namespace, false); cur = getChildElement("transition", element, namespace, false); if (cur != null) { String species = getChildText("species", cur, namespace, true); String trans = getChildText("transition", cur, namespace, true); nrg.transition = new EnergyTransition(species, trans); } return nrg; }
From source file:ca.nrc.cadc.caom2.xml.ObservationReader.java
License:Open Source License
protected Time getTime(Element parent, Namespace namespace, ReadContext rc) throws ObservationParsingException { Element element = getChildElement("time", parent, namespace, false); if (element == null) { return null; }//from w w w . j av a2s .co m Time tim = new Time(); Element cur = getChildElement("bounds", element, namespace, false); if (cur != null) { double lb = getChildTextAsDouble("lower", cur, namespace, true); double ub = getChildTextAsDouble("upper", cur, namespace, true); tim.bounds = new Interval(lb, ub); addSamples(tim.bounds, cur.getChild("samples", namespace), namespace, rc); } cur = getChildElement("dimension", element, namespace, false); if (cur != null) { // Attribute type = cur.getAttribute("type", xsiNamespace); // String tval = type.getValue(); // String extype = namespace.getPrefix() + ":" + // Long.class.getSimpleName(); // if ( extype.equals(tval) ) // { tim.dimension = getChildTextAsLong("dimension", element, namespace, true); // } // else // throw new ObservationParsingException("unsupported dimension // type: " + tval); } tim.resolution = getChildTextAsDouble("resolution", element, namespace, false); tim.sampleSize = getChildTextAsDouble("sampleSize", element, namespace, false); tim.exposure = getChildTextAsDouble("exposure", element, namespace, false); return tim; }
From source file:ca.nrc.cadc.caom2.xml.ObservationReader.java
License:Open Source License
protected Element getChildElement(String name, Element element, Namespace ns, boolean required) throws ObservationParsingException { Element child = element.getChild(name, ns); if (required && child == null) { String error = name + " element not found in " + element.getName(); throw new ObservationParsingException(error); }/*w ww .java 2 s . c o m*/ return child; }
From source file:ca.nrc.cadc.caom2.xml.ObservationReader.java
License:Open Source License
protected void addKeywordsToList(Collection<String> list, Element element, Namespace ns) throws ObservationParsingException { Element kwe = element.getChild("keywords", ns); log.debug("addKeywordsToList: " + kwe); if (kwe == null) { return;/*from w ww . j a v a2s. c o m*/ } List kws = kwe.getChildren("keyword", ns); log.debug("addKeywordsToList: " + kws.size()); Iterator it = kws.iterator(); while (it.hasNext()) { Element k = (Element) it.next(); String s = k.getTextTrim(); log.debug("addKeywordsToList: " + s); list.add(s); } }
From source file:ca.nrc.cadc.dali.tables.votable.VOTableReader.java
License:Open Source License
/** * Read a XML VOTable from a Reader and build a VOTable object. * * @param reader Reader to read from./*ww w . j a v a2 s . c o m*/ * @return a VOTable object. * @throws IOException if problem reading from the reader. */ public VOTable read(Reader reader) throws IOException { // Parse the input document. Document document; try { document = docBuilder.build(reader); } catch (JDOMException e) { throw new IOException("Unable to parse " + e.getMessage()); } // Returned VOTable object. VOTable votable = new VOTable(); // Document root element. Element root = document.getRootElement(); // Namespace for the root element. Namespace namespace = root.getNamespace(); log.debug("Namespace: " + namespace); // RESOURCE element. Element resource = root.getChild("RESOURCE", namespace); if (resource != null) { // Get the RESOURCE name attribute. Attribute resourceName = resource.getAttribute("name"); if (resourceName != null) { votable.setResourceName(resourceName.getValue()); } // INFO element. List<Element> infos = resource.getChildren("INFO", namespace); votable.getInfos().addAll(getInfos(infos, namespace)); // TABLE element. Element table = resource.getChild("TABLE", namespace); if (table != null) { // PARAM elements. List<Element> params = table.getChildren("PARAM", namespace); votable.getParams().addAll(getParams(params, namespace)); // FIELD elements. List<Element> fields = table.getChildren("FIELD", namespace); votable.getColumns().addAll(getFields(fields, namespace)); // DATA element. Element data = table.getChild("DATA", namespace); if (data != null) { // TABLEDATA element. Element tableData = data.getChild("TABLEDATA", namespace); votable.setTableData(getTableData(tableData, namespace, votable.getColumns())); } } } return votable; }
From source file:ca.nrc.cadc.dali.tables.votable.VOTableReader.java
License:Open Source License
/** * Populate a TableField object with values from the FIELD element. * * @param tableField TableField to populate. * @param element source Element./*from w w w . j a v a 2 s . co m*/ * @param namespace document namespace. */ protected void updateTableField(TableField tableField, Element element, Namespace namespace) { tableField.id = element.getAttributeValue("ID"); tableField.ucd = element.getAttributeValue("ucd"); tableField.unit = element.getAttributeValue("unit"); tableField.utype = element.getAttributeValue("utype"); tableField.xtype = element.getAttributeValue("xtype"); String arraysize = element.getAttributeValue("arraysize"); if (arraysize != null) { int index = arraysize.indexOf("*"); if (index == -1) { tableField.variableSize = Boolean.FALSE; } else { arraysize = arraysize.substring(0, index); tableField.variableSize = Boolean.TRUE; } if (!arraysize.trim().isEmpty()) { tableField.arraysize = Integer.parseInt(arraysize); } } // DESCRIPTION element for the FIELD. Element description = element.getChild("DESCRIPTION", namespace); if (description != null) { tableField.description = description.getText(); } // VALUES element for the PARAM. Element values = element.getChild("VALUES", namespace); if (values != null) { List<Element> options = values.getChildren("OPTION", namespace); if (!options.isEmpty()) { tableField.values = new ArrayList<String>(); for (Element option : options) { tableField.values.add(option.getAttributeValue("value")); } } } }
From source file:ca.nrc.cadc.uws.JobListReader.java
License:Open Source License
private List<JobRef> parseJobList(Document doc) throws ParseException, DataConversionException { Element root = doc.getRootElement(); List<Element> children = root.getChildren(); Iterator<Element> childIterator = children.iterator(); List<JobRef> jobs = new ArrayList<JobRef>(); Element next = null; JobRef jobRef = null;/*from ww w . j av a2 s . c om*/ ExecutionPhase executionPhase = null; Date creationTime = null; Attribute nil = null; String runID = null; String ownerID = null; while (childIterator.hasNext()) { next = childIterator.next(); String jobID = next.getAttributeValue("id"); Element phaseElement = next.getChild(JobAttribute.EXECUTION_PHASE.getAttributeName(), UWS.NS); String phase = phaseElement.getValue(); executionPhase = ExecutionPhase.valueOf(phase); Element creationTimeElement = next.getChild(JobAttribute.CREATION_TIME.getAttributeName(), UWS.NS); String time = creationTimeElement.getValue(); creationTime = dateFormat.parse(time); Element runIDElement = next.getChild(JobAttribute.RUN_ID.getAttributeName(), UWS.NS); nil = runIDElement.getAttribute("nil", UWS.XSI_NS); if (nil != null && nil.getBooleanValue()) runID = null; else runID = runIDElement.getTextTrim(); Element ownerIDElement = next.getChild(JobAttribute.OWNER_ID.getAttributeName(), UWS.NS); ownerID = ownerIDElement.getTextTrim(); jobRef = new JobRef(jobID, executionPhase, creationTime, runID, ownerID); jobs.add(jobRef); } return jobs; }