Java Code Examples for javax.xml.parsers.DocumentBuilderFactory.setIgnoringElementContentWhitespace()

The following are Jave code examples for showing how to use setIgnoringElementContentWhitespace() of the javax.xml.parsers.DocumentBuilderFactory class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: MAL-   File: MalGet.java   View Source Code Vote up 7 votes
public Document get(String url) throws Exception{
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    String user = api.getUser();String password = api.getPassword();
    String authStr = user + ":" + password;
    String authEncoded = Base64.encodeBase64String(authStr.getBytes());
    con.setRequestMethod("GET");
    con.setRequestProperty("Authorization", "Basic " + authEncoded);
    con.setDoOutput(true);
    DocumentBuilderFactory factoryBuilder = DocumentBuilderFactory.newInstance();
    factoryBuilder.setIgnoringComments(true);
    factoryBuilder.setIgnoringElementContentWhitespace(true);
    DocumentBuilder d = factoryBuilder.newDocumentBuilder();
    Document document = d.parse(con.getInputStream());
    return document;
}
 
Example 2
Project: butterfly   File: CompareXMLFiles.java   View Source Code Vote up 6 votes
@Override
protected boolean compare(File baselineFile, File comparisonFile) {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        factory.setNamespaceAware(true);
        factory.setCoalescing(true);
        factory.setIgnoringElementContentWhitespace(true);
        factory.setIgnoringComments(true);

        DocumentBuilder builder = factory.newDocumentBuilder();
        Document baselineXml = builder.parse(baselineFile);
        Document comparisonXml = builder.parse(comparisonFile);

        baselineXml.normalizeDocument();
        comparisonXml.normalizeDocument();

        XMLUnit.setIgnoreAttributeOrder(true);
        XMLUnit.setIgnoreComments(true);
        XMLUnit.setIgnoreWhitespace(true);

        return XMLUnit.compareXML(baselineXml, comparisonXml).similar();
    } catch (SAXException | IOException | ParserConfigurationException e) {
        throw new TransformationUtilityException("An exception happened when comparing the two XML files", e);
    }
}
 
Example 3
Project: openjdk-jdk10   File: JAXPTestUtilities.java   View Source Code Vote up 6 votes
/**
 * Compare contents of golden file with test output file by their document
 * representation.
 * Here we ignore the white space and comments. return true if they're
 * lexical identical.
 * @param goldfile Golden output file name.
 * @param resultFile Test output file name.
 * @return true if two file's document representation are identical.
 *         false if two file's document representation are not identical.
 * @throws javax.xml.parsers.ParserConfigurationException if the
 *         implementation is not available or cannot be instantiated.
 * @throws SAXException If any parse errors occur.
 * @throws IOException if an I/O error occurs reading from the file or a
 *         malformed or unmappable byte sequence is read .
 */
public static boolean compareDocumentWithGold(String goldfile, String resultFile)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setCoalescing(true);
    factory.setIgnoringElementContentWhitespace(true);
    factory.setIgnoringComments(true);
    DocumentBuilder db = factory.newDocumentBuilder();

    Document goldD = db.parse(Paths.get(goldfile).toFile());
    goldD.normalizeDocument();
    Document resultD = db.parse(Paths.get(resultFile).toFile());
    resultD.normalizeDocument();
    return goldD.isEqualNode(resultD);
}
 
Example 4
Project: tomcat7   File: DomUtil.java   View Source Code Vote up 6 votes
/** Read XML as DOM.
 */
public static Document readXml(InputStream is)
    throws SAXException, IOException, ParserConfigurationException
{
    DocumentBuilderFactory dbf =
        DocumentBuilderFactory.newInstance();

    dbf.setValidating(false);
    dbf.setIgnoringComments(false);
    dbf.setIgnoringElementContentWhitespace(true);
    //dbf.setCoalescing(true);
    //dbf.setExpandEntityReferences(true);

    DocumentBuilder db = null;
    db = dbf.newDocumentBuilder();
    db.setEntityResolver( new NullResolver() );

    // db.setErrorHandler( new MyErrorHandler());

    Document doc = db.parse(is);
    return doc;
}
 
Example 5
Project: openjdk-jdk10   File: DocumentBuilderFactoryTest.java   View Source Code Vote up 6 votes
/**
 * Test for the isIgnoringElementContentWhitespace and the
 * setIgnoringElementContentWhitespace. The xml file has all kinds of
 * whitespace,tab and newline characters, it uses the MyNSContentHandler
 * which does not invoke the characters callback when this
 * setIgnoringElementContentWhitespace is set to true.
 * @throws Exception If any errors occur.
 */
@Test
public void testCheckElementContentWhitespace() throws Exception {
    String goldFile = GOLDEN_DIR + "dbfactory02GF.out";
    String outputFile = USER_DIR + "dbfactory02.out";
    MyErrorHandler eh = MyErrorHandler.newInstance();
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(true);
    assertFalse(dbf.isIgnoringElementContentWhitespace());
    dbf.setIgnoringElementContentWhitespace(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    db.setErrorHandler(eh);
    Document doc = db.parse(new File(XML_DIR, "DocumentBuilderFactory06.xml"));
    assertFalse(eh.isErrorOccured());
    DOMSource domSource = new DOMSource(doc);
    TransformerFactory tfactory = TransformerFactory.newInstance();
    Transformer transformer = tfactory.newTransformer();
    SAXResult saxResult = new SAXResult();
    try(MyCHandler handler = MyCHandler.newInstance(new File(outputFile))) {
        saxResult.setHandler(handler);
        transformer.transform(domSource, saxResult);
    }
    assertTrue(compareWithGold(goldFile, outputFile));
}
 
Example 6
Project: xmlrss   File: RedactableXMLSignature.java   View Source Code Vote up 6 votes
/**
 * Loads the document used for signing/verification/redaction from the given input stream.
 * <p>
 * The document is checked against the given schema and the given error handler is used.
 *
 * @param inputStream the input stream
 * @param schema      the schema of the loaded XML document
 * @param handler     the errorhandler
 * @throws RedactableXMLSignatureException if this RedactableXMLSignature object is not properly initialized
 */
public final void setDocument(InputStream inputStream, Schema schema, ErrorHandler handler)
        throws RedactableXMLSignatureException {

    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setSchema(schema);
    documentBuilderFactory.setNamespaceAware(true);
    documentBuilderFactory.setIgnoringElementContentWhitespace(true);

    try {
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        documentBuilder.setErrorHandler(handler);
        setDocument(documentBuilder.parse(inputStream));
    } catch (ParserConfigurationException | SAXException | IOException e) {
        throw new RedactableXMLSignatureException(e);
    }
}
 
Example 7
Project: FlexibleRichTextView   File: GlueSettingsParser.java   View Source Code Vote up 6 votes
public GlueSettingsParser() throws ResourceParseException {
	try {
		setTypeMappings();
		setStyleMappings();
		DocumentBuilderFactory factory = DocumentBuilderFactory
				.newInstance();
		factory.setIgnoringElementContentWhitespace(true);
		factory.setIgnoringComments(true);
		root = factory.newDocumentBuilder()
				.parse(AjLatexMath.getAssetManager().open(RESOURCE_NAME))
				.getDocumentElement();
		parseGlueTypes();
	} catch (Exception e) { // JDOMException or IOException
		throw new XMLResourceParseException(RESOURCE_NAME, e);
	}
}
 
Example 8
Project: xquery-engine   File: XPathEvaluator.java   View Source Code Vote up 6 votes
/**
 * Reads a XML file and loads its content into a list of nodes
 *
 * @param fn Name of the XML file (relative to the executable's current path)
 * @return Root of the XML tree corresponding to the loaded document, as a singleton list of nodes
 */
public static LinkedList<Node> root(String fn) {
    LinkedList<Node> nodes = new LinkedList<>();
    try {
        // Remove quotes (first and last character)
        File xmlFile = new File(fn.substring(1, fn.length() - 1));
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        // Ignore non-relevant whitespace (only works if the XML has an associated DTD)
        docFactory.setIgnoringElementContentWhitespace(true);
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(xmlFile);
        // Normalize document
        doc.getDocumentElement().normalize();
        nodes.add(doc);
    } catch (Exception e) {
        return new LinkedList<>();
    }
    return nodes;
}
 
Example 9
Project: lazycat   File: DomUtil.java   View Source Code Vote up 6 votes
/**
 * Read XML as DOM.
 */
public static Document readXml(InputStream is) throws SAXException, IOException, ParserConfigurationException {
	DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

	dbf.setValidating(false);
	dbf.setIgnoringComments(false);
	dbf.setIgnoringElementContentWhitespace(true);
	// dbf.setCoalescing(true);
	// dbf.setExpandEntityReferences(true);

	DocumentBuilder db = null;
	db = dbf.newDocumentBuilder();
	db.setEntityResolver(new NullResolver());

	// db.setErrorHandler( new MyErrorHandler());

	Document doc = db.parse(is);
	return doc;
}
 
Example 10
Project: xquery-engine   File: XQueryTests.java   View Source Code Vote up 6 votes
/**
 * Loads a XML
 *
 * @param name Name of the resource (without path)
 * @return List of nodes corresponding to the children of the root of the loaded document
 * @throws Exception Exception if input resource is not found or it has invalid format
 */
LinkedList<Node> loadResourceAsXML(String name) throws Exception {
    FileInputStream xmlFile = getResource(name);
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    // Ignore non-relevant whitespace
    docFactory.setIgnoringElementContentWhitespace(true);
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(xmlFile);
    // Normalize document
    doc.getDocumentElement().normalize();
    // Add the children (XML resource should have a root which is ignored)
    LinkedList<Node> nodes = new LinkedList<>();
    NodeList nl = doc.getDocumentElement().getChildNodes();
    for (int i = 0; i < nl.getLength(); ++i) {
        nodes.add(nl.item(i));
    }
    return nodes;
}
 
Example 11
Project: apache-tomcat-7.0.73-with-comment   File: DomUtil.java   View Source Code Vote up 6 votes
/** Read XML as DOM.
 */
public static Document readXml(InputStream is)
    throws SAXException, IOException, ParserConfigurationException
{
    DocumentBuilderFactory dbf =
        DocumentBuilderFactory.newInstance();

    dbf.setValidating(false);
    dbf.setIgnoringComments(false);
    dbf.setIgnoringElementContentWhitespace(true);
    //dbf.setCoalescing(true);
    //dbf.setExpandEntityReferences(true);

    DocumentBuilder db = null;
    db = dbf.newDocumentBuilder();
    db.setEntityResolver( new NullResolver() );

    // db.setErrorHandler( new MyErrorHandler());

    Document doc = db.parse(is);
    return doc;
}
 
Example 12
Project: lams   File: BasicParserPool.java   View Source Code Vote up 6 votes
/**
 * Initializes the pool with a new set of configuration options.
 * 
 * @throws XMLParserException thrown if there is a problem initialzing the pool
 */
protected synchronized void initializePool() throws XMLParserException {
    if (!dirtyBuilderConfiguration) {
        // in case the pool was initialized by some other thread
        return;
    }

    DocumentBuilderFactory newFactory = DocumentBuilderFactory.newInstance();
    setAttributes(newFactory, builderAttributes);
    setFeatures(newFactory, builderFeatures);
    newFactory.setCoalescing(coalescing);
    newFactory.setExpandEntityReferences(expandEntityReferences);
    newFactory.setIgnoringComments(ignoreComments);
    newFactory.setIgnoringElementContentWhitespace(ignoreElementContentWhitespace);
    newFactory.setNamespaceAware(namespaceAware);
    newFactory.setSchema(schema);
    newFactory.setValidating(dtdValidating);
    newFactory.setXIncludeAware(xincludeAware);

    poolVersion++;
    dirtyBuilderConfiguration = false;
    builderFactory = newFactory;
    builderPool.clear();
}
 
Example 13
Project: lams   File: DomUtil.java   View Source Code Vote up 6 votes
/** Read XML as DOM.
 */
public static Document readXml(InputStream is)
    throws SAXException, IOException, ParserConfigurationException
{
    DocumentBuilderFactory dbf =
        DocumentBuilderFactory.newInstance();

    dbf.setValidating(false);
    dbf.setIgnoringComments(false);
    dbf.setIgnoringElementContentWhitespace(true);
    //dbf.setCoalescing(true);
    //dbf.setExpandEntityReferences(true);

    DocumentBuilder db = null;
    db = dbf.newDocumentBuilder();
    db.setEntityResolver( new NullResolver() );

    // db.setErrorHandler( new MyErrorHandler());

    Document doc = db.parse(is);
    return doc;
}
 
Example 14
Project: LibraSock   File: XmlUtils.java   View Source Code Vote up 5 votes
public static Document loadString(String paramString) throws Exception {
	DocumentBuilderFactory localDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
	localDocumentBuilderFactory.setIgnoringComments(false);
	localDocumentBuilderFactory.setIgnoringElementContentWhitespace(false);
	localDocumentBuilderFactory.setValidating(false);
	localDocumentBuilderFactory.setCoalescing(false);
	DocumentBuilder localDocumentBuilder = localDocumentBuilderFactory.newDocumentBuilder();
	char[] arrayOfChar = new char[paramString.length()];
	paramString.getChars(0, paramString.length(), arrayOfChar, 0);
	InputSource localInputSource = new InputSource(new CharArrayReader(arrayOfChar));
	return localDocumentBuilder.parse(localInputSource);
}
 
Example 15
Project: FlexibleRichTextView   File: TeXSymbolParser.java   View Source Code Vote up 5 votes
public TeXSymbolParser(InputStream file, String name)
		throws ResourceParseException {
	try {
		DocumentBuilderFactory factory = DocumentBuilderFactory
				.newInstance();
		factory.setIgnoringElementContentWhitespace(true);
		factory.setIgnoringComments(true);
		root = factory.newDocumentBuilder().parse(file)
				.getDocumentElement();
		// set possible valid symbol type mappings
		setTypeMappings();
	} catch (Exception e) { // JDOMException or IOException
		throw new XMLResourceParseException(name, e);
	}
}
 
Example 16
Project: joai-project   File: XMLDocReader.java   View Source Code Vote up 5 votes
/**
 *  Gets a org.w3c.dom.Document for this record. This method is optimized to create only one DOM when
 *  accessed multiple times for the same XMLDocReader.
 *
 * @return    A org.w3c.dom.Document, or null if unable to read.
 */
public org.w3c.dom.Document getW3CXmlDoc() {
	if (w3cXmlDoc != null)
		return w3cXmlDoc;

	DocumentBuilderFactory docfactory
		 = DocumentBuilderFactory.newInstance();
	docfactory.setCoalescing(true);
	docfactory.setExpandEntityReferences(true);
	docfactory.setIgnoringComments(true);

	docfactory.setNamespaceAware(true);

	// We must set validation false since jdk1.4 parser
	// doesn't know about schemas.
	docfactory.setValidating(false);

	// Ignore whitespace doesn't work unless setValidating(true),
	// according to javadocs.
	docfactory.setIgnoringElementContentWhitespace(false);
	try {
		DocumentBuilder docbuilder = docfactory.newDocumentBuilder();
		w3cXmlDoc = docbuilder.parse(getXml());
	} catch (Throwable e) {
		return null;
	}
	return w3cXmlDoc;
}
 
Example 17
Project: oscm   File: XMLConverter.java   View Source Code Vote up 5 votes
/**
 * Converts a given string into its document representation.
 * 
 * @param string
 *            The String to be converted.
 * @param nameSpaceAware
 *            Indicates whether namespaces have to be considered or not.
 * @return The document representation of the string, <code>null</code> in
 *         case the input string was <code>null</code>.
 * @throws ParserConfigurationException
 *             Thrown in case the conversion cannot be initiated.
 * @throws IOException
 *             Thrown in case the reading of the string fails.
 * @throws SAXException
 *             Thrown in case the string cannot be parsed.
 */
public static Document convertToDocument(String string,
        boolean nameSpaceAware) throws ParserConfigurationException,
        SAXException, IOException {
    if (string == null) {
        return null;
    }
    DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
    dfactory.setNamespaceAware(nameSpaceAware);
    dfactory.setValidating(false);
    dfactory.setIgnoringElementContentWhitespace(true);
    dfactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
    DocumentBuilder builder = dfactory.newDocumentBuilder();
    Document doc = builder.parse(new InputSource(new StringReader(string)));
    return doc;
}
 
Example 18
Project: lams   File: StaticBasicParserPool.java   View Source Code Vote up 5 votes
/**
 * Initializes the pool with a new set of configuration options.
 * 
 * @throws XMLParserException thrown if there is a problem initialzing the pool
 */
protected synchronized void initializeFactory() throws XMLParserException {
    DocumentBuilderFactory newFactory = DocumentBuilderFactory.newInstance();
    setAttributes(newFactory, builderAttributes);
    setFeatures(newFactory, builderFeatures);
    newFactory.setCoalescing(coalescing);
    newFactory.setExpandEntityReferences(expandEntityReferences);
    newFactory.setIgnoringComments(ignoreComments);
    newFactory.setIgnoringElementContentWhitespace(ignoreElementContentWhitespace);
    newFactory.setNamespaceAware(namespaceAware);
    newFactory.setSchema(schema);
    newFactory.setValidating(dtdValidating);
    newFactory.setXIncludeAware(xincludeAware);
    builderFactory = newFactory;
}
 
Example 19
Project: LibraSock   File: XmlUtils.java   View Source Code Vote up 5 votes
public static Document blankDocument(String paramString) throws Exception {
	DocumentBuilderFactory localDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
	localDocumentBuilderFactory.setIgnoringComments(false);
	localDocumentBuilderFactory.setIgnoringElementContentWhitespace(false);
	localDocumentBuilderFactory.setValidating(false);
	localDocumentBuilderFactory.setCoalescing(false);
	DocumentBuilder localDocumentBuilder = localDocumentBuilderFactory.newDocumentBuilder();
	Document localDocument = localDocumentBuilder.newDocument();
	Element localElement = localDocument.createElement(paramString);
	localDocument.appendChild(localElement);
	return localDocument;
}
 
Example 20
Project: LibraSock   File: XmlUtils.java   View Source Code Vote up 5 votes
public static Document load(File paramFile) throws Exception {
	DocumentBuilderFactory localDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
	localDocumentBuilderFactory.setIgnoringComments(false);
	localDocumentBuilderFactory.setIgnoringElementContentWhitespace(false);
	localDocumentBuilderFactory.setValidating(false);
	localDocumentBuilderFactory.setCoalescing(true);
	DocumentBuilder localDocumentBuilder = localDocumentBuilderFactory.newDocumentBuilder();
	return localDocumentBuilder.parse(paramFile);
}