List of usage examples for javax.xml.parsers ParserConfigurationException getMessage
public String getMessage()
From source file:org.sakaiproject.assignment.impl.conversion.AssignmentSubmissionAccess.java
/** * @param xml/*from ww w . j a va2 s . c om*/ * @throws EntityParseException */ public void parse(String xml) throws Exception { Reader r = new StringReader(xml); InputSource ss = new InputSource(r); SAXParser p = null; if (parserFactory == null) { parserFactory = SAXParserFactory.newInstance(); parserFactory.setNamespaceAware(false); parserFactory.setValidating(false); } try { p = parserFactory.newSAXParser(); } catch (ParserConfigurationException e) { log.warn("{}:parse {}", this, e.getMessage()); throw new SAXException("Failed to get a parser ", e); } final Map<String, Object> props = new HashMap<String, Object>(); saxSerializableProperties.setSerializableProperties(props); p.parse(ss, new DefaultHandler() { /* * (non-Javadoc) * * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, * java.lang.String, java.lang.String, org.xml.sax.Attributes) */ @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if ("property".equals(qName)) { String name = attributes.getValue("name"); String enc = StringUtils.trimToNull(attributes.getValue("enc")); String value = null; if ("BASE64".equalsIgnoreCase(enc)) { String charset = StringUtils.trimToNull(attributes.getValue("charset")); if (charset == null) charset = "UTF-8"; value = Xml.decode(charset, attributes.getValue("value")); } else { value = attributes.getValue("value"); } // deal with multiple valued lists if ("list".equals(attributes.getValue("list"))) { // accumulate multiple values in a list Object current = props.get(name); // if we don't have a value yet, make a list to // hold // this one if (current == null) { List values = new ArrayList(); props.put(name, values); values.add(value); } // if we do and it's a list, add this one else if (current instanceof List) { ((List) current).add(value); } // if it's not a list, it's wrong! else { log.warn("construct(el): value set not a list: {}", name); } } else { props.put(name, value); } } else if ("submission".equals(qName)) { setId(attributes.getValue("id")); setAssignment(StringUtils.trimToNull(attributes.getValue("assignment"))); setContext(StringUtils.trimToNull(attributes.getValue("context"))); setDatereturned(StringUtils.trimToNull(attributes.getValue("datereturned"))); setDatesubmitted(StringUtils.trimToNull(attributes.getValue("datesubmitted"))); setFeedbackcomment(StringUtils.trimToNull(attributes.getValue("feedbackcomment"))); if (StringUtils.trimToNull(attributes.getValue("feedbackcomment-html")) != null) { setFeedbackcomment_html( StringUtils.trimToNull(attributes.getValue("feedbackcomment-html"))); } else if (StringUtils.trimToNull(attributes.getValue("feedbackcomment-formatted")) != null) { setFeedbackcomment_html( StringUtils.trimToNull(attributes.getValue("feedbackcomment-formatted"))); } setFeedbacktext(StringUtils.trimToNull(attributes.getValue("feedbacktext"))); if (StringUtils.trimToNull(attributes.getValue("feedbacktext-html")) != null) { setFeedbacktext_html(StringUtils.trimToNull(attributes.getValue("feedbacktext-html"))); } else if (StringUtils.trimToNull(attributes.getValue("feedbacktext-formatted")) != null) { setFeedbacktext_html(StringUtils.trimToNull(attributes.getValue("feedbacktext-formatted"))); } // get number of decimals String factor = StringUtils.trimToNull(attributes.getValue("scaled_factor")); if (factor == null) { factor = String.valueOf(AssignmentConstants.DEFAULT_SCALED_FACTOR); } m_factor = Integer.valueOf(factor); // get grade String grade = StringUtils.trimToNull(attributes.getValue("scaled_grade")); if (grade == null) { grade = StringUtils.trimToNull(attributes.getValue("grade")); if (grade != null) { try { Integer.parseInt(grade); // for the grades in points, multiple those by factor grade = grade + factor.substring(1); } catch (Exception e) { log.warn("{}:parse grade {}", this, e.getMessage()); } } } setGrade(grade); setGraded(StringUtils.trimToNull(attributes.getValue("graded"))); setGradedBy(StringUtils.trimToNull(attributes.getValue("gradedBy"))); setGradereleased(StringUtils.trimToNull(attributes.getValue("gradereleased"))); setLastmod(StringUtils.trimToNull(attributes.getValue("lastmod"))); addElementsToList("feedbackattachment", feedbackattachments, attributes, false); addElementsToList("submittedattachment", submittedattachments, attributes, false); setPledgeflag(StringUtils.trimToNull(attributes.getValue("pledgeflag"))); setReturned(StringUtils.trimToNull(attributes.getValue("returned"))); setReviewReport(StringUtils.trimToNull(attributes.getValue("reviewReport"))); setReviewScore(StringUtils.trimToNull(attributes.getValue("reviewScore"))); setReviewStatus(StringUtils.trimToNull(attributes.getValue("reviewStatus"))); setSubmitted(StringUtils.trimToNull(attributes.getValue("submitted"))); // submittedtext and submittedtext_html are base-64 setSubmittedtext(StringUtils.trimToNull(attributes.getValue("submittedtext"))); setSubmittedtext_html(StringUtils.trimToNull(attributes.getValue("submittedtext-html"))); setSubmitterId(StringUtils.trimToNull(attributes.getValue("submitterid"))); addElementsToList("submitter", submitters, attributes, false); // for backward compatibility of assignments without submitter ids if (getSubmitterId() == null && submitters.size() > 0) { setSubmitterId(submitters.get(0)); } addElementsToList("grade", grades, attributes, false); } } }); }
From source file:org.sakaiproject.assignment.impl.conversion.impl.AssignmentSubmissionAccess.java
/** * @param xml/*from w ww . j a v a 2s . c om*/ * @throws EntityParseException */ public void parse(String xml) throws Exception { Reader r = new StringReader(xml); InputSource ss = new InputSource(r); SAXParser p = null; if (parserFactory == null) { parserFactory = SAXParserFactory.newInstance(); parserFactory.setNamespaceAware(false); parserFactory.setValidating(false); } try { p = parserFactory.newSAXParser(); } catch (ParserConfigurationException e) { log.warn(this + ":parse " + e.getMessage()); throw new SAXException("Failed to get a parser ", e); } final Map<String, Object> props = new HashMap<String, Object>(); saxSerializableProperties.setSerializableProperties(props); p.parse(ss, new DefaultHandler() { /* * (non-Javadoc) * * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, * java.lang.String, java.lang.String, org.xml.sax.Attributes) */ @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if ("property".equals(qName)) { String name = attributes.getValue("name"); String enc = StringUtils.trimToNull(attributes.getValue("enc")); String value = null; if ("BASE64".equalsIgnoreCase(enc)) { String charset = StringUtils.trimToNull(attributes.getValue("charset")); if (charset == null) charset = "UTF-8"; value = Xml.decode(charset, attributes.getValue("value")); } else { value = attributes.getValue("value"); } // deal with multiple valued lists if ("list".equals(attributes.getValue("list"))) { // accumulate multiple values in a list Object current = props.get(name); // if we don't have a value yet, make a list to // hold // this one if (current == null) { List values = new ArrayList(); props.put(name, values); values.add(value); } // if we do and it's a list, add this one else if (current instanceof List) { ((List) current).add(value); } // if it's not a list, it's wrong! else { log.warn("construct(el): value set not a list: " + name); } } else { props.put(name, value); } } else if ("submission".equals(qName)) { setId(attributes.getValue("id")); setAssignment(StringUtils.trimToNull(attributes.getValue("assignment"))); setContext(StringUtils.trimToNull(attributes.getValue("context"))); setDatereturned(StringUtils.trimToNull(attributes.getValue("datereturned"))); setDatesubmitted(StringUtils.trimToNull(attributes.getValue("datesubmitted"))); setFeedbackcomment(StringUtils.trimToNull(attributes.getValue("feedbackcomment"))); if (StringUtils.trimToNull(attributes.getValue("feedbackcomment-html")) != null) { setFeedbackcomment_html( StringUtils.trimToNull(attributes.getValue("feedbackcomment-html"))); } else if (StringUtils.trimToNull(attributes.getValue("feedbackcomment-formatted")) != null) { setFeedbackcomment_html( StringUtils.trimToNull(attributes.getValue("feedbackcomment-formatted"))); } setFeedbacktext(StringUtils.trimToNull(attributes.getValue("feedbacktext"))); if (StringUtils.trimToNull(attributes.getValue("feedbacktext-html")) != null) { setFeedbacktext_html(StringUtils.trimToNull(attributes.getValue("feedbacktext-html"))); } else if (StringUtils.trimToNull(attributes.getValue("feedbacktext-formatted")) != null) { setFeedbacktext_html(StringUtils.trimToNull(attributes.getValue("feedbacktext-formatted"))); } // get number of decimals String factor = StringUtils.trimToNull(attributes.getValue("scaled_factor")); if (factor == null) { factor = String.valueOf(AssignmentConstants.DEFAULT_SCALED_FACTOR); } m_factor = Integer.valueOf(factor); // get grade String grade = StringUtils.trimToNull(attributes.getValue("scaled_grade")); if (grade == null) { grade = StringUtils.trimToNull(attributes.getValue("grade")); if (grade != null) { try { Integer.parseInt(grade); // for the grades in points, multiple those by factor grade = grade + factor.substring(1); } catch (Exception e) { log.warn(this + ":parse grade " + e.getMessage()); } } } setGrade(grade); setGraded(StringUtils.trimToNull(attributes.getValue("graded"))); setGradedBy(StringUtils.trimToNull(attributes.getValue("gradedBy"))); setGradereleased(StringUtils.trimToNull(attributes.getValue("gradereleased"))); setLastmod(StringUtils.trimToNull(attributes.getValue("lastmod"))); addElementsToList("feedbackattachment", feedbackattachments, attributes, false); addElementsToList("submittedattachment", submittedattachments, attributes, false); setPledgeflag(StringUtils.trimToNull(attributes.getValue("pledgeflag"))); setReturned(StringUtils.trimToNull(attributes.getValue("returned"))); setReviewReport(StringUtils.trimToNull(attributes.getValue("reviewReport"))); setReviewScore(StringUtils.trimToNull(attributes.getValue("reviewScore"))); setReviewStatus(StringUtils.trimToNull(attributes.getValue("reviewStatus"))); setSubmitted(StringUtils.trimToNull(attributes.getValue("submitted"))); // submittedtext and submittedtext_html are base-64 setSubmittedtext(StringUtils.trimToNull(attributes.getValue("submittedtext"))); setSubmittedtext_html(StringUtils.trimToNull(attributes.getValue("submittedtext-html"))); setSubmitterId(StringUtils.trimToNull(attributes.getValue("submitterid"))); addElementsToList("submitter", submitters, attributes, false); addElementsToList("log", submissionLog, attributes, false); addElementsToList("grade", grades, attributes, false); } } }); }
From source file:org.sakaiproject.contentreview.turnitin.util.TurnitinAPIUtil.java
public static Document callTurnitinReturnDocument(String apiURL, Map<String, Object> parameters, String secretKey, int timeout, Proxy proxy, boolean isMultipart) throws TransientSubmissionException, SubmissionException { InputStream inputStream = callTurnitinReturnInputStream(apiURL, parameters, secretKey, timeout, proxy, isMultipart);/* ww w.jav a 2 s . c om*/ BufferedReader in; in = new BufferedReader(new InputStreamReader(inputStream)); Document document = null; try { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder parser = documentBuilderFactory.newDocumentBuilder(); document = parser.parse(new org.xml.sax.InputSource(in)); } catch (ParserConfigurationException pce) { log.error("parser configuration error: " + pce.getMessage()); throw new TransientSubmissionException("Parser configuration error", pce); } catch (Exception t) { throw new TransientSubmissionException("Cannot parse Turnitin response. Assuming call was unsuccessful", t); } if (apiTraceLog.isDebugEnabled()) { apiTraceLog.debug(" Result from call: " + Xml.writeDocumentToString(document)); } return document; }
From source file:org.sakaiproject.site.tool.SiteInfoToolServlet.java
/** * Initialize this servlet.//from w ww. ja v a2 s. c o m */ public void init() throws ServletException { super.init(); try { basicAuth = new BasicAuth(); basicAuth.init(); transformerFactory = TransformerFactory.newInstance(); try { docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); } catch (ParserConfigurationException e) { log.warn(this + " cannot get DocumentBuilder " + e.getMessage()); } } catch (Exception e) { log.warn(this + "init " + e.getMessage()); } }
From source file:org.sakaiproject.tool.assessment.business.entity.RecordingData.java
/** * This takes a RecordingData object and puts it in XML. * @return the XML as an org.w3c.dom.Document *//*from w w w . j av a2 s .c om*/ public Document getXMLDataModel() { Log log = LogFactory.getLog(RecordingData.class); Document document = null; DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setNamespaceAware(true); try { DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder(); document = documentBuilder.newDocument(); } catch (ParserConfigurationException e) { log.error(e.getMessage(), e); } if (document == null) { log.error("document is null"); return null; } //add audio setup data to XML document //root Element recordingData = document.createElement("RecordingData"); //sub elements Element agentName = document.createElement("AgentName"); Element agentId = document.createElement("AgentId"); Element courseAssignmentContext = document.createElement("CourseAssignmentContext"); Element fileExtension = document.createElement("FileExtension"); Element fileName = document.createElement("FileName"); Element limit = document.createElement("Limit"); Element dir = document.createElement("Dir"); Element seconds = document.createElement("Seconds"); Element appName = document.createElement("AppName"); Element imageURL = document.createElement("ImageURL"); agentName.appendChild(document.createTextNode(this.getAgentName())); agentId.appendChild(document.createTextNode(this.getAgentId())); courseAssignmentContext.appendChild(document.createTextNode(this.getCourseAssignmentContext())); fileExtension.appendChild(document.createTextNode(this.getFileExtension())); fileName.appendChild(document.createTextNode(this.getFileName())); limit.appendChild(document.createTextNode(this.getLimit())); dir.appendChild(document.createTextNode(this.getDir())); seconds.appendChild(document.createTextNode(this.getSeconds())); appName.appendChild(document.createTextNode(this.getAppName())); imageURL.appendChild(document.createTextNode(this.getImageURL())); recordingData.appendChild(agentName); recordingData.appendChild(agentId); recordingData.appendChild(courseAssignmentContext); recordingData.appendChild(fileExtension); recordingData.appendChild(fileName); recordingData.appendChild(limit); recordingData.appendChild(dir); recordingData.appendChild(seconds); recordingData.appendChild(appName); recordingData.appendChild(imageURL); document.appendChild(recordingData); // return the recording data available in XML return document; }
From source file:org.sakaiproject.tool.assessment.contentpackaging.Manifest.java
/** * Add a section ref with section Id sectionId. * * @param sectionId// ww w .ja va 2 s.c om */ public void addSectionRef(String sectionId) { if (log.isDebugEnabled()) { log.debug("addSection(String " + sectionId + ")"); } try { String xpath = basePath; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.newDocument(); Element element = document.createElement(QTIConstantStrings.SECTIONREF); element.setAttribute(QTIConstantStrings.LINKREFID, sectionId); this.addElement(xpath, element); } catch (ParserConfigurationException pce) { log.error("Exception thrown from addSectionRef() : " + pce.getMessage()); pce.printStackTrace(); } }
From source file:org.sakaiproject.tool.assessment.qti.asi.ASIBaseClass.java
/** * Set field entry./*from w w w . j a v a2s. c o m*/ * * @param xpath * @param setValue * @param noEscapeXML */ protected void setFieldentry(String xpath, String value, boolean noEscapeXML) { String setValue = null; if (noEscapeXML) { setValue = value; } else { setValue = escapeXml(value); } if (log.isDebugEnabled()) { log.debug("setFieldentry(String " + xpath + ", String " + setValue + ")"); } List metadataList; try { metadataList = this.selectNodes(xpath); int no = metadataList.size(); String val = null; if (metadataList.size() > 0) { Document document = this.getDocument(); Element fieldentry = (Element) metadataList.get(0); CharacterData fieldentryText = (CharacterData) fieldentry.getFirstChild(); Integer getTime = null; if ((fieldentryText != null) && (fieldentryText.getNodeValue() != null) && (fieldentryText.getNodeValue().trim().length() > 0)) { val = fieldentryText.getNodeValue(); } if (setValue != null) { if (fieldentryText == null) { Text newElementText = fieldentry.getOwnerDocument().createTextNode(setValue); fieldentry.appendChild(newElementText); fieldentryText = (CharacterData) fieldentry.getFirstChild(); } else { fieldentryText.setNodeValue(setValue); } } } } catch (ParserConfigurationException e) { log.error(e.getMessage(), e); } catch (SAXException e) { log.error(e.getMessage(), e); } catch (IOException e) { log.error(e.getMessage(), e); } }
From source file:org.sakaiproject.tool.assessment.qti.asi.ASIBaseClass.java
/** * * * @param xpath//from w w w. ja va 2 s .c o m * @param fieldlabel */ protected void createFieldentry(String xpath, String fieldlabel) { if (log.isDebugEnabled()) { log.debug("createFieldentry(String " + xpath + ", String " + fieldlabel + ")"); } try { List qtimetadataNodes = this.selectNodes(xpath); if (qtimetadataNodes.size() > 0) { Node qtimetadataNode = (Node) qtimetadataNodes.get(0); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document newDocument = db.newDocument(); Element qtimetadataField = newDocument.createElement(QTIConstantStrings.QTIMETADATAFIELD); Element fieldlabelElement = newDocument.createElement(QTIConstantStrings.FIELDLABEL); Element fieldentryElement = newDocument.createElement(QTIConstantStrings.FIELDENTRY); Text fieldlabelText = newDocument.createTextNode(QTIConstantStrings.FIELDLABEL); fieldlabelText.setNodeValue(fieldlabel); fieldlabelElement.appendChild(fieldlabelText); Text fieldentryText = newDocument.createTextNode(QTIConstantStrings.FIELDENTRY); fieldentryElement.appendChild(fieldentryText); Node importedFLE = qtimetadataField.getOwnerDocument().importNode(fieldlabelElement, true); Node importedFEE = qtimetadataField.getOwnerDocument().importNode(fieldentryElement, true); qtimetadataField.appendChild(importedFLE); qtimetadataField.appendChild(importedFEE); Node importedField = qtimetadataNode.getOwnerDocument().importNode(qtimetadataField, true); qtimetadataNode.appendChild(importedField); } } catch (ParserConfigurationException pce) { log.error("Exception thrown from createFieldentry()" + pce.getMessage(), pce); pce.printStackTrace(); } catch (Exception ex) { log.error(ex.getMessage(), ex); } }
From source file:org.sakaiproject.tool.assessment.qti.asi.ASIBaseClass.java
protected void wrappingMattext() { log.debug("wrappingMattext()"); try {/*from ww w . ja v a 2 s .c o m*/ NodeList list = this.getDocument().getElementsByTagName(QTIConstantStrings.MATTEXT); int size = list.getLength(); for (int i = 0; i < size; i++) { Node node = list.item(i); Node childNode = node.getFirstChild(); if ((childNode != null) && childNode instanceof CharacterData) { CharacterData cdi = (CharacterData) childNode; String data = cdi.getData(); //modify this string; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); Comment comment = doc.createComment(data); node.appendChild(node.getOwnerDocument().importNode(comment, true)); cdi.setData(""); } } } catch (ParserConfigurationException e) { log.error(e.getMessage(), e); } catch (SAXException e) { log.error(e.getMessage(), e); } catch (IOException e) { log.error(e.getMessage(), e); } }
From source file:org.sakaiproject.tool.assessment.qti.asi.Section.java
/** * ASI OKI implementation//from w ww . j a v a 2s . c om * * @param itemId item id */ public void addItemRef(String itemId) { if (log.isDebugEnabled()) { log.debug("addItem(String " + itemId + ")"); } try { String xpath = basePath; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.newDocument(); Element element = document.createElement(QTIConstantStrings.ITEMREF); element.setAttribute(QTIConstantStrings.LINKREFID, itemId); this.addElement(xpath, element); } catch (ParserConfigurationException pce) { log.error("Exception thrown from addItemRef() : " + pce.getMessage()); pce.printStackTrace(); } }