List of usage examples for org.jdom2 Element getNamespaceURI
public String getNamespaceURI()
From source file:qtiscoringengine.QTIRubric.java
License:Open Source License
public static QTIRubric fromXML(String source, XmlReader reader, ValidationLog log) { Document doc = null;/*from www. j a va 2 s .c o m*/ try { doc = reader.getDocument(); } catch (Exception e) { log.addMessage(null, "Failed to load document. Message: " + e.getMessage()); return null; } // this should never be null at this point, but just in case i guess... if (doc == null) { log.addMessage(null, "Failed to load document. The XmlDocument is null. Sorry for lack of diagnostics."); return null; } Element foo = doc.getRootElement(); if (foo == null) { log.addMessage(null, "Failed to load document. There is no DocumentElement."); return null; } // Create an XmlNamespaceManager for resolving namespaces. XmlNamespaceManager nsmgr = new XmlNamespaceManager(/* doc.NameTable */); boolean foundNamespace = false; // add a default value for the namespace nsmgr.addNamespace("qti", QTIXmlConstants.NameSpaces[0]); String xmlns = foo.getNamespaceURI(); XmlElement fooAsXml = new XmlElement(foo); if (StringHelper.contains(QTIXmlConstants.NameSpaces, xmlns)) {/* contains */ nsmgr.addNamespace("qti", xmlns); foundNamespace = true; } else { // XmlNodeList namespaceNodes = foo.SelectNodes("//*[@xmlns]"); List<Element> namespaceNodes = fooAsXml.selectNodes("//*[@xmlns]"); for (Element elem : namespaceNodes) // if // (QTIXmlConstants.NameSpaces.Contains(elem.getAttributeValue("xmlns"))) if (QTIXmlConstants.containsSchemaLocation(elem.getAttributeValue("xmlns"))) {/* contains */ nsmgr.addNamespace("qti", elem.getAttributeValue("xmlns")); foundNamespace = true; break; } } List<Element> responseDeclarations = fooAsXml.selectNodes(QTIXmlConstants.ResponseDeclaration, nsmgr); List<Element> outcomeDeclarations = fooAsXml.selectNodes(QTIXmlConstants.OutcomeDeclaration, nsmgr); Element responseProcessing = fooAsXml.selectSingleNode(QTIXmlConstants.ResponseProcessing, nsmgr); if (responseProcessing != null) { String url = responseProcessing.getAttributeValue("template"); if (!StringUtils.isEmpty(url)) { try { responseProcessing = QTIUtility.getXMLFromURL(url, nsmgr); } catch (Exception e) { // responseProcessing can be the root node, so to keep us from // printing the entire // xml file we just add the error message without a node log.addMessage(null, "Error getting responseProcessing node. Url='" + url + "', message: " + e.getMessage()); } } } List<ResponseDeclaration> rdList = new ArrayList<ResponseDeclaration>(); for (Element node : responseDeclarations) { try { ResponseDeclaration rd = ResponseDeclaration.fromXML(node, nsmgr, log); if (rd != null) rdList.add(rd); } catch (Exception e) { log.addMessage(node, e.getMessage()); } } List<OutcomeDeclaration> odList = new ArrayList<OutcomeDeclaration>(); for (Element node : outcomeDeclarations) { try { OutcomeDeclaration od = OutcomeDeclaration.fromXML(node, nsmgr, log); if (od != null) odList.add(od); } catch (Exception e) { log.addMessage(node, e.getMessage()); } } ResponseProcessing responseProcessor = null; try { responseProcessor = ResponseProcessing.fromXml(responseProcessing, nsmgr, log); } catch (Exception e) { e.printStackTrace(); log.addMessage(responseProcessing, e.getMessage()); } // if the rubric is empty then it might be because the namespace wasn't // recognized // as an allowed value. Let the user know. if (rdList.size() == 0 && odList.size() == 0 && responseProcessor == null && !foundNamespace) log.addMessage(null, "No valid namespace was found in this XML. Maybe you need to add it to the allowed values in QTIXmlConstants?"); QTIRubric rubric = new QTIRubric(source, rdList, odList, responseProcessor); return rubric; }