List of usage examples for javax.xml.parsers DocumentBuilderFactory setIgnoringElementContentWhitespace
public void setIgnoringElementContentWhitespace(boolean whitespace)
From source file:org.terracotta.config.TCConfigurationParser.java
@SuppressWarnings("unchecked") private static TcConfiguration parseStream(InputStream in, ErrorHandler eh, String source, ClassLoader loader) throws IOException, SAXException { Collection<Source> schemaSources = new ArrayList<>(); for (ServiceConfigParser parser : loadConfigurationParserClasses(loader)) { schemaSources.add(parser.getXmlSchema()); serviceParsers.put(parser.getNamespace(), parser); }//from w ww . j a v a 2 s . co m schemaSources.add(new StreamSource(TERRACOTTA_XML_SCHEMA.openStream())); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); factory.setIgnoringComments(true); factory.setIgnoringElementContentWhitespace(true); factory.setSchema(XSD_SCHEMA_FACTORY.newSchema(schemaSources.toArray(new Source[schemaSources.size()]))); final DocumentBuilder domBuilder; try { domBuilder = factory.newDocumentBuilder(); } catch (ParserConfigurationException e) { throw new AssertionError(e); } domBuilder.setErrorHandler(eh); final Element config = domBuilder.parse(in).getDocumentElement(); try { JAXBContext jc = JAXBContext.newInstance("org.terracotta.config", TCConfigurationParser.class.getClassLoader()); Unmarshaller u = jc.createUnmarshaller(); TcConfig tcConfig = u.unmarshal(config, TcConfig.class).getValue(); if (tcConfig.getServers() == null) { Servers servers = new Servers(); tcConfig.setServers(servers); } if (tcConfig.getServers().getServer().isEmpty()) { tcConfig.getServers().getServer().add(new Server()); } DefaultSubstitutor.applyDefaults(tcConfig); applyPlatformDefaults(tcConfig, source); Map<String, Map<String, ServiceOverride>> serviceOverrides = new HashMap<>(); for (Server server : tcConfig.getServers().getServer()) { if (server.getServiceOverrides() != null && server.getServiceOverrides().getServiceOverride() != null) { for (ServiceOverride serviceOverride : server.getServiceOverrides().getServiceOverride()) { String id = ((Service) serviceOverride.getOverrides()).getId(); if (serviceOverrides.get(id) == null) { serviceOverrides.put(id, new HashMap<>()); } serviceOverrides.get(id).put(server.getName(), serviceOverride); } } } Map<String, List<ServiceProviderConfiguration>> serviceConfigurations = new HashMap<>(); if (tcConfig.getServices() != null && tcConfig.getServices().getService() != null) { //now parse the service configuration. for (Service service : tcConfig.getServices().getService()) { Element element = service.getAny(); if (element != null) { URI namespace = URI.create(element.getNamespaceURI()); ServiceConfigParser parser = serviceParsers.get(namespace); if (parser == null) { throw new TCConfigurationSetupException("Can't find parser for service " + namespace); } ServiceProviderConfiguration serviceProviderConfiguration = parser.parse(element, source); for (Server server : tcConfig.getServers().getServer()) { if (serviceConfigurations.get(server.getName()) == null) { serviceConfigurations.put(server.getName(), new ArrayList<>()); } if (serviceOverrides.get(service.getId()) != null && serviceOverrides.get(service.getId()).containsKey(server.getName())) { Element overrideElement = serviceOverrides.get(service.getId()) .get(server.getName()).getAny(); if (overrideElement != null) { serviceConfigurations.get(server.getName()) .add(parser.parse(overrideElement, source)); } } else { serviceConfigurations.get(server.getName()).add(serviceProviderConfiguration); } } } } } return new TcConfiguration(tcConfig, source, serviceConfigurations); } catch (JAXBException e) { throw new TCConfigurationSetupException(e); } }
From source file:org.wso2.bps.samples.migration.MigrationExecutor.java
/** * Create DB connection// ww w .j a v a 2 s . c om * @return Connection * @throws ParserConfigurationException * @throws IOException * @throws SAXException * @throws ClassNotFoundException * @throws SQLException */ private static Connection initializeDBConnection() throws ParserConfigurationException, IOException, SAXException, ClassNotFoundException, SQLException { String databaseUsername = null; String databasePassword = null; String databaseDriver = null; boolean dbConfigFound = false; String configPath = System.getProperty("carbon.home") + File.separator + "repository" + File.separator + "conf" + File.separator + "datasources" + File.separator + "bps-datasources.xml"; System.out.println("Using datasource config file at :" + configPath); File elementXmlFile = new File(configPath); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); dbFactory.setIgnoringComments(true); dbFactory.setIgnoringElementContentWhitespace(true); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document document = dBuilder.parse(elementXmlFile); document.getDocumentElement().normalize(); NodeList datasourceList = document.getDocumentElement().getElementsByTagName("datasource"); for (int i = 0; i < datasourceList.getLength(); i++) { Node datasource = datasourceList.item(i); String dbName = ((DeferredElementImpl) datasource).getElementsByTagName("name").item(0) .getTextContent(); if (dbName.equals("BPS_DS")) { databaseURL = document.getDocumentElement().getElementsByTagName("url").item(i).getTextContent() .split(";")[0]; databaseDriver = document.getDocumentElement().getElementsByTagName("driverClassName").item(i) .getTextContent(); databaseUsername = document.getDocumentElement().getElementsByTagName("username").item(i) .getTextContent(); databasePassword = document.getDocumentElement().getElementsByTagName("password").item(i) .getTextContent(); dbConfigFound = true; break; } } if (!dbConfigFound) { System.out.println("DB configurations not found or invalid!"); System.exit(0); } Class.forName(databaseDriver); return DriverManager.getConnection(databaseURL, databaseUsername, databasePassword); }
From source file:org.wso2.bps.samples.processcleanup.CleanupExecutor.java
/** * Create DB connection/*from ww w .ja v a 2s . c om*/ * * @return Connection * @throws ParserConfigurationException * @throws IOException * @throws SAXException * @throws ClassNotFoundException * @throws SQLException */ private static Connection initializeDBConnection() throws ParserConfigurationException, IOException, SAXException, ClassNotFoundException, SQLException { String databaseUsername = null; String databasePassword = null; String databaseDriver = null; boolean dbConfigFound = false; bpsHome = System.getProperty(CleanupConstants.CARBON_HOME); if (!(bpsHome.endsWith(File.separator))) { bpsHome += File.separator; } System.out.println("Processcleanuptool startup - BPS HOME DIRECTORY : " + bpsHome); String configPath = bpsHome + CleanupConstants.REPOSITORY + File.separator + CleanupConstants.CONF + File.separator + CleanupConstants.DATASOURCES + File.separator + CleanupConstants.BPS_DATASOURCES; File elementXmlFile = new File(configPath); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); dbFactory.setIgnoringComments(true); dbFactory.setIgnoringElementContentWhitespace(true); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document document = dBuilder.parse(elementXmlFile); document.getDocumentElement().normalize(); NodeList datasourceList = document.getDocumentElement().getElementsByTagName(CleanupConstants.DATASOURCE); for (int i = 0; i < datasourceList.getLength(); i++) { Node datasource = datasourceList.item(i); String dbName = ((DeferredElementImpl) datasource).getElementsByTagName(CleanupConstants.NAME).item(0) .getTextContent(); if (dbName.equals(CleanupConstants.BPS_DS)) { databaseURL = document.getDocumentElement().getElementsByTagName(CleanupConstants.URL).item(i) .getTextContent().split(";")[0]; databaseDriver = document.getDocumentElement() .getElementsByTagName(CleanupConstants.DRIVER_CLASS_NAME).item(i).getTextContent(); databaseUsername = document.getDocumentElement().getElementsByTagName(CleanupConstants.USER_NAME) .item(i).getTextContent(); databasePassword = document.getDocumentElement().getElementsByTagName(CleanupConstants.PASSWORD) .item(i).getTextContent(); dbConfigFound = true; break; } } if (!dbConfigFound) { log.error("DB configurations not found or invalid!"); System.exit(0); } Class.forName(databaseDriver); return DriverManager.getConnection(databaseURL, databaseUsername, databasePassword); }
From source file:org.wso2.ei.businessprocess.utils.migration.MigrationExecutor.java
/** * Create DB connection/*from www .ja va 2 s.co m*/ * @return Connection * @throws ParserConfigurationException * @throws IOException * @throws SAXException * @throws ClassNotFoundException * @throws SQLException */ private static Connection initializeDBConnection() throws ParserConfigurationException, IOException, SAXException, ClassNotFoundException, SQLException { String databaseUsername = null; String databasePassword = null; String databaseDriver = null; boolean dbConfigFound = false; String configPath = System.getProperty("carbon.home") + File.separator + "conf" + File.separator + "datasources" + File.separator + "bps-datasources.xml"; System.out.println("Using datasource config file at :" + configPath); File elementXmlFile = new File(configPath); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); dbFactory.setIgnoringComments(true); dbFactory.setIgnoringElementContentWhitespace(true); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document document = dBuilder.parse(elementXmlFile); document.getDocumentElement().normalize(); NodeList datasourceList = document.getDocumentElement().getElementsByTagName("datasource"); for (int i = 0; i < datasourceList.getLength(); i++) { Node datasource = datasourceList.item(i); String dbName = ((DeferredElementImpl) datasource).getElementsByTagName("name").item(0) .getTextContent(); if (dbName.equals("BPS_DS")) { databaseURL = document.getDocumentElement().getElementsByTagName("url").item(i).getTextContent() .split(";")[0]; databaseDriver = document.getDocumentElement().getElementsByTagName("driverClassName").item(i) .getTextContent(); databaseUsername = document.getDocumentElement().getElementsByTagName("username").item(i) .getTextContent(); databasePassword = document.getDocumentElement().getElementsByTagName("password").item(i) .getTextContent(); dbConfigFound = true; break; } } if (!dbConfigFound) { System.out.println("DB configurations not found or invalid!"); System.exit(0); } Class.forName(databaseDriver); return DriverManager.getConnection(databaseURL, databaseUsername, databasePassword); }
From source file:org.wso2.ei.businessprocess.utils.processcleanup.CleanupExecutor.java
/** * Create DB connection/*from www . jav a2 s. c om*/ * * @return Connection * @throws ParserConfigurationException * @throws IOException * @throws SAXException * @throws ClassNotFoundException * @throws SQLException */ private static Connection initializeDBConnection() throws ParserConfigurationException, IOException, SAXException, ClassNotFoundException, SQLException { String databaseUsername = null; String databasePassword = null; String databaseDriver = null; boolean dbConfigFound = false; bpsHome = System.getProperty(CleanupConstants.CARBON_HOME); if (!(bpsHome.endsWith(File.separator))) { bpsHome += File.separator; } System.out.println("Processcleanuptool startup - BPS HOME DIRECTORY : " + bpsHome); String configPath = bpsHome + File.separator + CleanupConstants.CONF + File.separator + CleanupConstants.DATASOURCES + File.separator + CleanupConstants.BPS_DATASOURCES; File elementXmlFile = new File(configPath); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); dbFactory.setIgnoringComments(true); dbFactory.setIgnoringElementContentWhitespace(true); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document document = dBuilder.parse(elementXmlFile); document.getDocumentElement().normalize(); NodeList datasourceList = document.getDocumentElement().getElementsByTagName(CleanupConstants.DATASOURCE); for (int i = 0; i < datasourceList.getLength(); i++) { Node datasource = datasourceList.item(i); String dbName = ((DeferredElementImpl) datasource).getElementsByTagName(CleanupConstants.NAME).item(0) .getTextContent(); if (dbName.equals(CleanupConstants.BPS_DS)) { databaseURL = document.getDocumentElement().getElementsByTagName(CleanupConstants.URL).item(i) .getTextContent().split(";")[0]; databaseDriver = document.getDocumentElement() .getElementsByTagName(CleanupConstants.DRIVER_CLASS_NAME).item(i).getTextContent(); databaseUsername = document.getDocumentElement().getElementsByTagName(CleanupConstants.USER_NAME) .item(i).getTextContent(); databasePassword = document.getDocumentElement().getElementsByTagName(CleanupConstants.PASSWORD) .item(i).getTextContent(); dbConfigFound = true; break; } } if (!dbConfigFound) { log.error("DB configurations not found or invalid!"); System.exit(0); } Class.forName(databaseDriver); return DriverManager.getConnection(databaseURL, databaseUsername, databasePassword); }
From source file:ru.codeinside.gses.webui.utils.JarParseUtils.java
public static Document readXml(InputStream is) throws SAXException, IOException, ParserConfigurationException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(false);/*from w w w . j a va 2 s.c o m*/ dbf.setIgnoringComments(false); dbf.setIgnoringElementContentWhitespace(true); dbf.setNamespaceAware(true); // dbf.setCoalescing(true); // dbf.setExpandEntityReferences(true); DocumentBuilder db = dbf.newDocumentBuilder(); db.setEntityResolver(new NullResolver()); // db.setErrorHandler( new MyErrorHandler()); return db.parse(is); }
From source file:ru.codeinside.gws.crypto.cryptopro.CryptoProvider.java
@Override public String signElement(String sourceXML, String elementName, String namespace, boolean removeIdAttribute, boolean signatureAfterElement, boolean inclusive) throws Exception { loadCertificate();//from www . j a v a 2s . c o m DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setIgnoringElementContentWhitespace(true); dbf.setCoalescing(true); dbf.setNamespaceAware(true); DocumentBuilder documentBuilder = dbf.newDocumentBuilder(); InputSource is = new InputSource(new StringReader(sourceXML)); Document doc = documentBuilder.parse(is); Element elementForSign = (Element) doc.getElementsByTagNameNS(namespace, elementName).item(0); Node parentNode = null; Element detachedElementForSign; Document detachedDocument; if (!elementForSign.isSameNode(doc.getDocumentElement())) { parentNode = elementForSign.getParentNode(); parentNode.removeChild(elementForSign); detachedDocument = documentBuilder.newDocument(); Node importedElementForSign = detachedDocument.importNode(elementForSign, true); detachedDocument.appendChild(importedElementForSign); detachedElementForSign = detachedDocument.getDocumentElement(); } else { detachedElementForSign = elementForSign; detachedDocument = doc; } String signatureMethodUri = inclusive ? "urn:ietf:params:xml:ns:cpxmlsec:algorithms:gostr34102001-gostr3411" : "http://www.w3.org/2001/04/xmldsig-more#gostr34102001-gostr3411"; String canonicalizationMethodUri = inclusive ? "http://www.w3.org/TR/2001/REC-xml-c14n-20010315" : "http://www.w3.org/2001/10/xml-exc-c14n#"; XMLSignature sig = new XMLSignature(detachedDocument, "", signatureMethodUri, canonicalizationMethodUri); if (!removeIdAttribute) { detachedElementForSign.setAttribute("Id", detachedElementForSign.getTagName()); } if (signatureAfterElement) detachedElementForSign.insertBefore(sig.getElement(), detachedElementForSign.getLastChild().getNextSibling()); else { detachedElementForSign.insertBefore(sig.getElement(), detachedElementForSign.getFirstChild()); } Transforms transforms = new Transforms(detachedDocument); transforms.addTransform("http://www.w3.org/2000/09/xmldsig#enveloped-signature"); transforms.addTransform(inclusive ? "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments" : "http://www.w3.org/2001/10/xml-exc-c14n#"); String digestURI = inclusive ? "urn:ietf:params:xml:ns:cpxmlsec:algorithms:gostr3411" : "http://www.w3.org/2001/04/xmldsig-more#gostr3411"; sig.addDocument(removeIdAttribute ? "" : "#" + detachedElementForSign.getTagName(), transforms, digestURI); sig.addKeyInfo(cert); sig.sign(privateKey); if ((!elementForSign.isSameNode(doc.getDocumentElement())) && (parentNode != null)) { Node signedNode = doc.importNode(detachedElementForSign, true); parentNode.appendChild(signedNode); } TransformerFactory tf = TransformerFactory.newInstance(); Transformer trans = tf.newTransformer(); trans.setOutputProperty("omit-xml-declaration", "yes"); StringWriter stringWriter = new StringWriter(); StreamResult streamResult = new StreamResult(stringWriter); trans.transform(new DOMSource(doc), streamResult); return stringWriter.toString(); }
From source file:ru.codeinside.gws.crypto.cryptopro.CryptoProvider.java
private Document createDocumentFromFragment(List<QName> namespaces, String appData) throws SAXException, IOException, ParserConfigurationException { // ? ? ??? : final QName wsu = new QName(WSU, "wsu"); final QName ds = new QName("http://www.w3.org/2000/09/xmldsig#", "ds"); if (namespaces.indexOf(wsu) == -1) { namespaces.add(wsu);/* w ww . java 2 s.co m*/ } if (namespaces.indexOf(ds) == -1) { namespaces.add(ds); } final StringBuilder sb = new StringBuilder(); sb.append("<root"); for (final QName name : namespaces) { sb.append(" xmlns:"); sb.append(name.getLocalPart()); sb.append("=\""); sb.append(name.getNamespaceURI()); sb.append("\""); } sb.append(">"); sb.append(appData); sb.append("</root>"); final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setIgnoringElementContentWhitespace(true); factory.setCoalescing(true); factory.setNamespaceAware(true); return factory.newDocumentBuilder().parse(new ByteArrayInputStream(sb.toString().getBytes("UTF-8"))); }
From source file:ru.codeinside.gws3572c.GMPClientSignTest.java
private Document createDocumentFromElement(Element element) throws ParserConfigurationException, IOException, SAXException { final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // ? , ? ? ? XML- dbf.setIgnoringElementContentWhitespace(true); // ? , ? CDATA ? XML- dbf.setCoalescing(true);//from w w w . j a v a2s. com // ? , ? ?? XML- dbf.setNamespaceAware(true); InputSource is = new InputSource(new StringReader(convertElementToString(element))); return dbf.newDocumentBuilder().parse(is); }
From source file:tkwatch.Utilities.java
/** * Gets a working instance of a document builder. * /*from w w w .j a va 2s . co m*/ * @return The document builder instance. * @throws ParserConfigurationException */ public static final DocumentBuilder getDocumentBuilder() throws ParserConfigurationException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(false); dbf.setIgnoringComments(false); dbf.setIgnoringElementContentWhitespace(true); dbf.setNamespaceAware(true); DocumentBuilder builder = dbf.newDocumentBuilder(); builder.setEntityResolver(new NullResolver()); return builder; }