Example usage for javax.xml.parsers ParserConfigurationException getMessage

List of usage examples for javax.xml.parsers ParserConfigurationException getMessage

Introduction

In this page you can find the example usage for javax.xml.parsers ParserConfigurationException getMessage.

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:org.sakaiproject.tool.assessment.qti.util.XmlUtil.java

/**
 * Create document object/*w  w w .j av a 2  s  .  c o  m*/
 *
 * @return Document
 */
public static Document createDocument() {
    log.debug("createDocument()");

    Document document = null;
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setNamespaceAware(true);

    try {
        setDocumentBuilderFactoryFeatures(builderFactory);
        DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
        document = documentBuilder.newDocument();
    } catch (ParserConfigurationException e) {
        log.error(e.getMessage(), e);
    }

    return document;
}

From source file:org.sakaiproject.tool.assessment.qti.util.XmlUtil.java

/**
 * Create document object from XML string
 *
 * @return Document/*from ww w .ja  v a  2 s.  c  om*/
 */
public static Document createDocument(String xmlString) {
    log.debug("createDocument()");

    Document document = null;
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setNamespaceAware(true);

    try {
        setDocumentBuilderFactoryFeatures(builderFactory);
        DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
        document = documentBuilder.parse(xmlString);
    } catch (IOException ex) {
        log.error(ex.getMessage(), ex);
    } catch (SAXException ex) {
        log.error(ex.getMessage(), ex);
    } catch (ParserConfigurationException e) {
        log.error(e.getMessage(), e);
    }

    return document;
}

From source file:org.sakaiproject.tool.assessment.qti.util.XmlUtil.java

/**
 * Read document from ServletContext/context path
 *
 * @param context ServletContext//from w  w  w .java2  s.c  om
 * @param path path
 *
 * @return Document
 */
public static Document readDocument(ServletContext context, String path) {
    if (log.isDebugEnabled()) {
        log.debug("readDocument(String " + path + ")");
    }

    Document document = null;
    InputStream inputStream = context.getResourceAsStream(path);
    String fullpath = context.getRealPath(path);
    log.debug("readDocument(full path) " + fullpath + ")");
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setNamespaceAware(true);

    try {
        setDocumentBuilderFactoryFeatures(builderFactory);
        DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
        document = documentBuilder.parse(inputStream);
    } catch (ParserConfigurationException e) {
        log.error(e.getMessage(), e);
    } catch (SAXException e) {
        log.error(e.getMessage(), e);
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    }

    return document;
}

From source file:org.sakaiproject.tool.assessment.qti.util.XmlUtil.java

/**
 * Read document from within spring context path
 *
 * @param path path/*  w  ww  . j  a  v  a  2s. c o  m*/
 *
 * @return Document
 */
public static Document readDocument(String path) {
    if (log.isDebugEnabled()) {
        log.debug("readDocument(String " + path + ")");
    }

    Document document = null;
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setNamespaceAware(true);
    ClassPathResource resource = new ClassPathResource(path);

    try {

        InputStream inputStream = resource.getInputStream();

        setDocumentBuilderFactoryFeatures(builderFactory);
        DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
        document = documentBuilder.parse(inputStream);
    } catch (ParserConfigurationException e) {
        log.error(e.getMessage(), e);
    } catch (SAXException e) {
        log.error(e.getMessage(), e);
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    }

    return document;
}

From source file:org.sakaiproject.tool.assessment.qti.util.XmlUtil.java

public static DOMSource getDocumentSource(ServletContext context, String path) {
    if (log.isDebugEnabled()) {
        log.debug("readDocument(String " + path + ")");
    }//from w w w .  ja va 2  s  .c o m

    InputStream inputStream = context.getResourceAsStream(path);
    String realPath = null;
    try {
        realPath = context.getResource(path).toString();
    } catch (MalformedURLException e1) {
        log.debug(e1.getMessage(), e1);
    }
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setNamespaceAware(true);

    Document document = null;
    DOMSource source = null;
    try {
        setDocumentBuilderFactoryFeatures(builderFactory);
        DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
        document = documentBuilder.parse(inputStream);
        source = new DOMSource(document, realPath);
    } catch (ParserConfigurationException e) {
        log.error(e.getMessage(), e);
    } catch (SAXException e) {
        log.error(e.getMessage(), e);
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    }

    return source;
}

From source file:org.sakaiproject.tool.assessment.qti.util.XmlUtil.java

/**
 * This more forgiving version skips blank lines if trim = true
 * Otherwise it does a direct parse of the file.
 * @param path file path/*from  ww w.  j av  a  2  s .  c  om*/
 * @param trim trim blank lines true/false
 * @return
* @throws ParserConfigurationException 
* @throws SAXException 
* @throws IOException 
 */
public static Document readDocument(String path, boolean trim)
        throws ParserConfigurationException, SAXException, IOException {
    if (log.isDebugEnabled()) {
        log.debug("readDocument(String " + path + ")");
    }

    Document document = null;
    InputStream inputStream = null;
    BufferedReader in = null;
    try {
        if (trim) {
            in = new BufferedReader(new FileReader(path));

            StringBuilder buffer = new StringBuilder();
            String s = "";
            while ((s = in.readLine()) != null) {
                if (s.trim().length() > 0) // skip blank lines
                {
                    buffer.append(s);
                    buffer.append("\n");
                }
            }
            in.close();
            byte[] bytes = buffer.toString().getBytes();

            inputStream = new ByteArrayInputStream(bytes);
        } else {
            inputStream = new FileInputStream(path);
        }

        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        builderFactory.setNamespaceAware(true);
        setDocumentBuilderFactoryFeatures(builderFactory);
        DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
        document = documentBuilder.parse(inputStream);
    } catch (ParserConfigurationException e) {
        log.debug(e.getMessage(), e);
        throw (e);
    } catch (SAXException e) {
        log.debug(e.getMessage(), e);
        throw (e);
    } catch (IOException e) {
        log.debug(e.getMessage(), e);
        throw (e);
    } finally {

        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                log.debug(e.getMessage(), e);// tried
            }
        }
        if (in != null) {
            try {
                in.close();
            } catch (Exception e1) {
                log.debug(e1.getMessage(), e1);
            }
        }

    }

    return document;
}

From source file:org.sakaiproject.tool.help.RestContentProvider.java

/**
 * create document/* w ww  .  j  a  va 2  s.c o  m*/
 * @return document
 */
private static Document createDocument() {
    if (LOG.isDebugEnabled()) {
        LOG.debug("createDocument()");
    }

    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);
        e.printStackTrace();
    }

    return document;
}

From source file:org.sakaiproject.tool.help.RestContentProvider.java

/**
 * synchronize initialization of caching XSL
 * @param context/*from www. ja  v  a2s . c o m*/
 */
public static void initializeXsl(ServletContext context) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("initializeXsl(ServletContext " + context + ")");
    }

    if (XSL_INITIALIZED.booleanValue()) {
        return;
    } else {
        synchronized (XSL_INITIALIZED_LOCK) {
            if (!XSL_INITIALIZED.booleanValue()) {
                //read in and parse xsl
                InputStream iStreamPreprocess = null;
                InputStream iStreamAllInOne = null;
                try {
                    iStreamPreprocess = context.getResourceAsStream(XML_PREPROCESS_XSL);
                    iStreamAllInOne = context.getResourceAsStream(XML_KB_XSL);

                    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
                    builderFactory.setNamespaceAware(true);
                    DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
                    xslDocumentPreprocess = documentBuilder.parse(iStreamPreprocess);
                    xslDocumentAllInOne = documentBuilder.parse(iStreamAllInOne);
                } catch (ParserConfigurationException e) {
                    LOG.error(e.getMessage(), e);
                } catch (IOException e) {
                    LOG.error(e.getMessage(), e);
                } catch (SAXException e) {
                    LOG.error(e.getMessage(), e);
                }
                try {
                    iStreamPreprocess.close();
                    iStreamAllInOne.close();
                } catch (IOException e) {
                    LOG.error(e.getMessage(), e);
                }

                XSL_INITIALIZED = Boolean.TRUE;
            }
        }
    }
}

From source file:org.sakaiproject.tool.help.RestContentProvider.java

/**
 * get transformed document//from  w w w  . j a va  2s. c o  m
 * @param servlet context
 * @param sBuffer
 * @return
 */
private static Document getTransformedDocument(ServletContext context, StringBuilder sBuffer) {

    if (LOG.isDebugEnabled()) {
        LOG.debug("getTransformedDocument(ServletContext " + context + ", StringBuilder " + sBuffer + ")");
    }

    initializeXsl(context);

    Document result = null;
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = dbf.newDocumentBuilder();
        StringReader sReader = new StringReader(sBuffer.toString());
        InputSource is = new org.xml.sax.InputSource(sReader);
        Document xmlDocument = builder.parse(is);

        // test for kb error condition
        if (xmlDocument.getElementsByTagName("kberror").getLength() > 0) {
            result = createErrorDocument();
        } else {
            /** debugging
            OutputFormat format = new OutputFormat(xmlDocument);
            XMLSerializer output = new XMLSerializer(System.out, format);
            output.serialize(xmlDocument);
            */

            result = transformDocument(xmlDocument, xslDocumentPreprocess);
            result = transformDocument(result, xslDocumentAllInOne);
        }

        /** debugging  
        OutputFormat format = new OutputFormat(result);
        XMLSerializer output = new XMLSerializer(System.out, format);
        output.serialize(result);
        */

        addLinkToCss(result);

    } catch (ParserConfigurationException e) {
        LOG.error(e.getMessage(), e);
    } catch (IOException e) {
        LOG.error(e.getMessage(), e);
    } catch (SAXException e) {
        LOG.error(e.getMessage(), e);
    }
    return result;
}

From source file:org.sakaiproject.webservices.TestsAndQuizzes.java

/** 
 * createAsessmentFromExportFile - WS Endpoint, exposing the SamLite createImportedAssessment()
 *
 * @param   String sessionid      the id of a valid admin session
 * @param   String siteid         the enterprise/sakai id of the site to be archived
 * @param   String siteproperty      the property that holds the enterprise site id
 * @param   String xmlfile         path to the IMS QTI document containing the assessment
 * @return   boolean                    returns true if assessment created successfully, false if assessment is null
 * //from w w  w  . ja va  2 s .  c  om
 * @throws   AxisFault         WS TestsAndQuizzes.createAssessmentFromXml(): XmlUtil.createDocument() returned a null QTI Document
 *                   WS TestsAndQuizzes.createAssessmentFromXml(): XmlUtil.createDocument() ParserConfigurationException: 
 *                  WS TestsAndQuizzes.createAssessmentFromXml(): XmlUtil.createDocument() SaxException:
 *                  WS TestsAndQuizzes.createAssessmentFromXml(): XmlUtil.createDocument() IOException: 
 *
 */

@WebMethod
@Path("/createAssessmentFromExportFile")
@Produces("text/plain")
@GET
public boolean createAssessmentFromExportFile(
        @WebParam(name = "sessionid", partName = "sessionid") @QueryParam("sessionid") String sessionid,
        @WebParam(name = "siteid", partName = "siteid") @QueryParam("siteid") String siteid,
        @WebParam(name = "siteproperty", partName = "siteproperty") @QueryParam("siteproperty") String siteproperty,
        @WebParam(name = "xmlfile", partName = "xmlfile") @QueryParam("xmlfile") String xmlfile) {
    Session session = establishSession(sessionid);
    Document document = null;

    try {
        document = XmlUtil.readDocument(xmlfile, true);
    } catch (ParserConfigurationException pce) {
        LOG.error(
                "WS TestsAndQuizzes.createAssessmentFromExportFile(): XmlUtil.createDocument() ParserConfigurationException: "
                        + pce.getMessage(),
                pce);
        throw new RuntimeException(
                "WS TestsAndQuizzes.createAssessmentFromExportFile(): XmlUtil.createDocument() ParserConfigurationException: "
                        + pce.getMessage());
    } catch (SAXException saxe) {
        LOG.error("WS TestsAndQuizzes.createAssessmentFromExportFile(): XmlUtil.createDocument() SaxException: "
                + saxe.getMessage(), saxe);
        throw new RuntimeException(
                "WS TestsAndQuizzes.createAssessmentFromExportFile(): XmlUtil.createDocument() SaxException: "
                        + saxe.getMessage());
    } catch (IOException ioe) {
        LOG.error("WS TestsAndQuizzes.createAssessmentFromExportFile(): XmlUtil.createDocument() IOException: "
                + ioe.getMessage(), ioe);
        throw new RuntimeException(
                "WS TestsAndQuizzes.createAssessmentFromExportFile(): XmlUtil.createDocument() IOException: "
                        + ioe.getMessage());
    }
    if (document == null) {
        throw new RuntimeException(
                "WS TestsAndQuizzes.createAssessmentFromExportFile(): XmlUtil.createDocument() returned a null QTI Document");
    }

    return createAssessment(siteid, siteproperty, null, null, null, document);
}