List of usage examples for javax.xml.parsers ParserConfigurationException getMessage
public String getMessage()
From source file:org.openmicroscopy.shoola.env.data.model.FileObject.java
/** * Parses the image's description./*from w w w .j a v a 2s. c o m*/ * * @param xmlStr The string to parse. * @return See above. */ private Document xmlParser(String xmlStr) throws SAXException { InputSource stream = new InputSource(); stream.setCharacterStream(new StringReader(xmlStr)); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); DocumentBuilder builder; Document doc = null; try { builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); doc = builder.parse(stream); } catch (ParserConfigurationException e) { e.printStackTrace(pw); IJ.log(sw.toString()); } catch (IOException e) { e.printStackTrace(pw); IJ.log(sw.toString()); } finally { try { sw.close(); } catch (IOException e) { IJ.log("I/O Exception:" + e.getMessage()); } pw.close(); } return doc; }
From source file:org.openmrs.module.muzima.handler.XmlRegistrationQueueDataHandler.java
private Patient createPatientFromPayload(final String payload) { Patient unsavedPatient = new Patient(); try {/*from ww w . j ava 2 s . c o m*/ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.parse(new InputSource(new ByteArrayInputStream(payload.getBytes("utf-8")))); Element element = document.getDocumentElement(); element.normalize(); Node patientNode = document.getElementsByTagName("patient").item(0); NodeList patientElementNodes = patientNode.getChildNodes(); PersonName personName = new PersonName(); PatientIdentifier patientIdentifier = new PatientIdentifier(); for (int i = 0; i < patientElementNodes.getLength(); i++) { Node patientElementNode = patientElementNodes.item(i); if (patientElementNode.getNodeType() == Node.ELEMENT_NODE) { Element patientElement = (Element) patientElementNode; String tagName = patientElement.getTagName(); if (tagName.equals("patient.middle_name")) { personName.setMiddleName(patientElement.getTextContent()); } else if (tagName.equals("patient.given_name")) { personName.setGivenName(patientElement.getTextContent()); } else if (tagName.equals("patient.family_name")) { personName.setFamilyName(patientElement.getTextContent()); } else if (tagName.equals("patient_identifier.identifier_type_id")) { int identifierTypeId = Integer.parseInt(patientElement.getTextContent()); PatientIdentifierType identifierType = Context.getPatientService() .getPatientIdentifierType(identifierTypeId); if (identifierType == null) { queueProcessorException.addException(new Exception( "Unable to find patient identifier type with id: " + identifierTypeId)); } else { patientIdentifier.setIdentifierType(identifierType); } } else if (tagName.equals("patient.medical_record_number")) { patientIdentifier.setIdentifier(patientElement.getTextContent()); } else if (tagName.equals("patient.sex")) { unsavedPatient.setGender(patientElement.getTextContent()); } else if (tagName.equals("patient.birthdate")) { Date dob = parseDate(patientElement.getTextContent()); unsavedPatient.setBirthdate(dob); } else if (tagName.equals("patient.uuid")) { unsavedPatient.setUuid(patientElement.getTextContent()); setTemporaryPatientUuid(patientElement.getTextContent()); } else if (tagName.equals("patient.finger")) { savePatientsFinger(unsavedPatient, patientElement.getTextContent()); } else if (tagName.equals("patient.fingerprint")) { savePatientsFingerprint(unsavedPatient, patientElement.getTextContent()); } else if (tagName.equals("amrs_medical_record_number_identifier_type")) { extractIdentifier(unsavedPatient, patientElement, "AMRS Medical Record Number"); } else if (tagName.equals("ccc_identifier_type")) { extractIdentifier(unsavedPatient, patientElement, "CCC Number "); } else if (tagName.equals("hct_identifier_type")) { extractIdentifier(unsavedPatient, patientElement, "HCT ID"); } else if (tagName.equals("kni_identifier_type")) { extractIdentifier(unsavedPatient, patientElement, "KENYAN NATIONAL ID NUMBER"); } else if (tagName.equals("mtct_identifier_type")) { extractIdentifier(unsavedPatient, patientElement, "MTCT Plus ID"); } else if (tagName.equals("mtrh_hospital_number_identifier_type")) { extractIdentifier(unsavedPatient, patientElement, "MTRH Hospital Number"); } else if (tagName.equals("old_amrs_number_identifier_type")) { extractIdentifier(unsavedPatient, patientElement, "Old AMPATH Medical Record Number"); } else if (tagName.equals("pmtc_identifier_type")) { extractIdentifier(unsavedPatient, patientElement, "pMTCT ID"); } else if (tagName.startsWith("person_attribute")) { PersonService personService = Context.getPersonService(); int personAttributeTypeId = NumberUtils.toInt(tagName.replace("person_attribute", "")); PersonAttributeType personAttributeType = personService .getPersonAttributeType(personAttributeTypeId); if (personAttributeType == null) { queueProcessorException.addException(new Exception( "Unable to find attribute type with id: " + personAttributeTypeId)); } else { PersonAttribute personAttribute = new PersonAttribute(); personAttribute.setAttributeType(personAttributeType); personAttribute.setValue(patientElement.getTextContent()); unsavedPatient.addAttribute(personAttribute); } } } } Node encounterNode = document.getElementsByTagName("encounter").item(0); NodeList encounterElementNodes = encounterNode.getChildNodes(); for (int i = 0; i < encounterElementNodes.getLength(); i++) { Node encounterElementNode = encounterElementNodes.item(i); if (encounterElementNode.getNodeType() == Node.ELEMENT_NODE) { Element encounterElement = (Element) encounterElementNode; if (encounterElement.getTagName().equals("encounter.location_id")) { int locationId = Integer.parseInt(encounterElement.getTextContent()); Location location = Context.getLocationService().getLocation(locationId); if (location == null) { queueProcessorException .addException(new Exception("Unable to find location with id: " + locationId)); } else { patientIdentifier.setLocation(location); } for (PatientIdentifier identifier : unsavedPatient.getIdentifiers()) { identifier.setLocation(location); } } } } unsavedPatient.addName(personName); unsavedPatient.addIdentifier(patientIdentifier); } catch (ParserConfigurationException e) { queueProcessorException.addException(new Exception(e.getMessage())); } catch (SAXException e) { queueProcessorException.addException(new Exception(e.getMessage())); } catch (IOException e) { queueProcessorException.addException(new Exception(e.getMessage())); } return unsavedPatient; }
From source file:org.osaf.caldav4j.methods.MkTicketMethod.java
/** * Returns a TicketResponse from the response body. * //from w w w. ja v a 2s . c o m * @return * @throws IOException * @throws SAXException * @throws CalDAV4JProtocolException - * If the Response Body is not XML * @throws CalDAV4JException - * if the Response Body is unavailable */ public TicketResponse getResponseBodyAsTicketResponse() throws IOException, SAXException, CalDAV4JProtocolException, CalDAV4JException { Header header = getResponseHeader("Content-Type"); String contentType = (header != null) ? header.getValue() : ""; if (!contentType.startsWith("text/xml")) { log.error( "Content type must be \"text/xml\" to parse as an " + "xml resource. Type was: " + contentType); throw new CalDAV4JProtocolException( "Content type must be \"text/xml\" to parse as an " + "xml resource"); } InputStream inStream = getResponseBodyAsStream(); if (inStream != null) { if (builder == null) { try { // TODO: avoid the newInstance call for each method // instance for performance reasons. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); builder = factory.newDocumentBuilder(); } catch (ParserConfigurationException e) { throw new HttpException("XML Parser Configuration error: " + e.getMessage()); } } this.responseDocument = builder.parse(inStream); inStream.close(); } else { throw new CalDAV4JException("Response Body is not Available, Status Code: " + getStatusCode()); } int statusCode = getStatusCode(); if (WebdavStatus.SC_OK == statusCode) { Element docElement = this.responseDocument.getDocumentElement(); TicketResponse tr = XMLUtils.createTicketResponseFromDOM(docElement); return tr; } return null; }
From source file:org.osaf.cosmo.calendar.hcalendar.HCalendarParser.java
private void parse(InputSource in, ContentHandler handler) throws IOException, ParserException { try {//from www .j a v a 2s . com Document d = BUILDER_FACTORY.newDocumentBuilder().parse(in); buildCalendar(d, handler); } catch (ParserConfigurationException e) { throw new RuntimeException(e); } catch (SAXException e) { if (e instanceof SAXParseException) { SAXParseException pe = (SAXParseException) e; throw new ParserException("Could not parse XML", pe.getLineNumber(), e); } throw new ParserException(e.getMessage(), -1, e); } }
From source file:org.owasp.webscarab.plugin.saml.SamlModel.java
public SamlModel(FrameworkModel model) { this.model = model; this.samlConversationModel = new FilteredConversationModel(model, model.getConversationModel()) { @Override/*from ww w.java2 s. co m*/ public boolean shouldFilter(ConversationID id) { return !isSAMLMessage(id); } }; this.parsedDocuments = new MRUCache<ConversationID, Document>(8); DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setNamespaceAware(true); try { this.builder = builderFactory.newDocumentBuilder(); } catch (ParserConfigurationException ex) { throw new RuntimeException("parser config error: " + ex.getMessage(), ex); } }
From source file:org.pentaho.pat.server.util.serializer.QuerySerializer.java
public String createXML() throws Exception { if (this.query == null) throw new Exception("Query object can not be null"); try {/*from www . j a v a2 s . com*/ createDocument(); createDOMTree(); XMLOutputter serializer = new XMLOutputter(); Format format = Format.getPrettyFormat(); StringWriter st = new StringWriter(); serializer.setFormat(format); serializer.output(dom, st); return st.getBuffer().toString(); } catch (ParserConfigurationException e) { throw new Exception(e.getMessage(), e); } }
From source file:org.pentaho.reporting.tools.configeditor.model.ModuleNodeFactory.java
public void load(final InputStream in, final boolean append) throws IOException { if (append == false) { configEntryLookup.clear();//from w w w.j av a2 s .com } final ConfigDescriptionModel model = new ConfigDescriptionModel(); try { model.load(in); } catch (SAXException saxException) { final String error = messages.getString("ModuleNodeFactory.ERROR_0001_PARSE_FAILURE", saxException.getMessage()); //$NON-NLS-1$ ModuleNodeFactory.logger.error(error, saxException); throw new IOException(error); } catch (ParserConfigurationException pE) { final String error = messages.getString("ModuleNodeFactory.ERROR_0002_PARSER_CONFIG_ERROR", pE.getMessage()); //$NON-NLS-1$ ModuleNodeFactory.logger.error(error, pE); throw new IOException(error); } final ConfigDescriptionEntry[] entries = model.toArray(); for (int i = 0; i < entries.length; i++) { //Log.debug ("Entry: " + entries[i].getKeyName() + " registered"); configEntryLookup.put(entries[i].getKeyName(), entries[i]); } }
From source file:org.pepstock.jem.gwt.server.services.JobsManager.java
/** * Indents JCL content, for editing//from w w w . j a v a 2 s. c o m * @param content JCL * @return indented JCL * @throws ServiceMessageException * @throws Exception if any XML exception occurs */ public String indent(String content) throws ServiceMessageException { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new ReaderInputStream(new StringReader(content))); OutputFormat format = new OutputFormat(document); format.setLineWidth(65); format.setIndenting(true); format.setIndent(4); StringWriter writer = new StringWriter(); XMLSerializer serializer = new XMLSerializer(writer, format); serializer.serialize(document); return writer.toString(); } catch (ParserConfigurationException e) { throw new ServiceMessageException(UserInterfaceMessage.JEMG022E, e, e.getMessage()); } catch (SAXException e) { throw new ServiceMessageException(UserInterfaceMessage.JEMG022E, e, e.getMessage()); } catch (IOException e) { throw new ServiceMessageException(UserInterfaceMessage.JEMG022E, e, e.getMessage()); } }
From source file:org.pepstock.jem.node.executors.configuration.CheckHazelcastConfiguration.java
/** * Checks if content could be a Hazelcast configuration file * @return always TRUE/* www . j a v a 2 s. co m*/ * @throws ParserConfigurationException * @throws IOException * @throws SAXException * @throws Exception occurs if errors */ @Override public Boolean execute() throws ExecutorException { try { // XML syntax check // because Hazelcast continues if XMl error occurs DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); builder.parse(new ReaderInputStream(new StringReader(content))); return Boolean.TRUE; } catch (ParserConfigurationException e) { throw new ExecutorException(NodeMessage.JEMC241E, e, e.getMessage()); } catch (SAXException e) { throw new ExecutorException(NodeMessage.JEMC241E, e, e.getMessage()); } catch (IOException e) { throw new ExecutorException(NodeMessage.JEMC241E, e, e.getMessage()); } }
From source file:org.sakaibrary.xserver.XServer.java
/** * Creates a new XServer object ready to communicate with the * MetaLib X-server. Reads searchProperties, sets up SAX Parser, and * sets up session management for this object. */// ww w. ja va 2 s . c o m public XServer(String guid) throws XServerException { this.guid = guid; // setup the SAX parser SAXParserFactory factory; factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); try { saxParser = factory.newSAXParser(); } catch (SAXException sxe) { // Error generated by this application // (or a parser-initialization error) Exception x = sxe; if (sxe.getException() != null) { x = sxe.getException(); } LOG.warn("XServer() SAX exception in trying to get a new SAXParser " + "from SAXParserFactory: " + sxe.getMessage(), x); throw new RuntimeException("XServer() SAX exception: " + sxe.getMessage(), x); } catch (ParserConfigurationException pce) { // Parser with specified options can't be built LOG.warn("XServer() SAX parser cannot be built with specified options"); throw new RuntimeException( "XServer() SAX parser cannot be built with " + "specified options: " + pce.getMessage(), pce); } // load session state msm = MetasearchSessionManager.getInstance(); MetasearchSession metasearchSession = msm.getMetasearchSession(guid); if (metasearchSession == null) { // bad state management throw new RuntimeException("XServer() - cache MetasearchSession is " + "NULL :: guid is " + guid); } // get X-Server base URL xserverBaseUrl = metasearchSession.getBaseUrl(); if (!metasearchSession.isLoggedIn()) { // need to login username = metasearchSession.getUsername(); password = metasearchSession.getPassword(); if (!loginURL(username, password)) { // authorization failed throw new XServerException("XServer.loginURL()", "authorization failed."); } // login success metasearchSession.setLoggedIn(true); metasearchSession.setSessionId(sessionId); } // get search properties org.osid.shared.Properties searchProperties = metasearchSession.getSearchProperties(); try { searchSourceIds = (ArrayList) searchProperties.getProperty("searchSourceIds"); // empty TODO sortBy = (String) searchProperties.getProperty("sortBy"); pageSize = (Integer) searchProperties.getProperty("pageSize"); startRecord = (Integer) searchProperties.getProperty("startRecord"); } catch (org.osid.shared.SharedException se) { LOG.warn("XServer() failed to get search properties - will assign " + "defaults", se); } // assign defaults if necessary // TODO assign the updated values to the session... searchProperties is read-only, need to add additional fields to MetasearchSession. if (sortBy == null) { sortBy = "rank"; } if (pageSize == null) { pageSize = new Integer(10); } if (startRecord == null) { startRecord = new Integer(1); } // check args if (startRecord.intValue() <= 0) { LOG.warn("XServer() - startRecord must be set to 1 or higher."); startRecord = null; startRecord = new Integer(1); } // add/update this MetasearchSession in the cache msm.putMetasearchSession(guid, metasearchSession); }