1 /*******************************************************************************
2 * Copyright (c) 2013 University of Stuttgart.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * and the Apache License 2.0 which both accompany this distribution,
6 * and are available at http://www.eclipse.org/legal/epl-v10.html
7 * and http://www.apache.org/licenses/LICENSE-2.0
10 * Oliver Kopp - initial API and implementation
11 *******************************************************************************/
12 package org.eclipse.winery.common;
16 import javax.xml.XMLConstants;
17 import javax.xml.parsers.DocumentBuilder;
18 import javax.xml.parsers.DocumentBuilderFactory;
19 import javax.xml.parsers.ParserConfigurationException;
20 import javax.xml.validation.Schema;
21 import javax.xml.validation.SchemaFactory;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.xml.sax.SAXException;
28 * Class to produce DocumentBuilders with a pre-loaded TOSCA XSD.
30 * In a separate class as TOSCA XSD loading takes a few seconds
32 public class TOSCADocumentBuilderFactory {
34 private static final Logger logger = LoggerFactory.getLogger(TOSCADocumentBuilderFactory.class);
36 public static final TOSCADocumentBuilderFactory INSTANCE = new TOSCADocumentBuilderFactory();
37 private final DocumentBuilderFactory factory;
40 public TOSCADocumentBuilderFactory() {
41 this.factory = DocumentBuilderFactory.newInstance();
43 this.factory.setNamespaceAware(true);
45 // we do not need DTD validation
46 this.factory.setValidating(false);
48 // we do XSD validation
49 SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
51 URL resource = this.getClass().getResource("/TOSCA-v1.0.xsd");
53 // takes a few seconds to load
54 schema = schemaFactory.newSchema(resource);
55 this.factory.setSchema(schema);
56 } catch (SAXException e) {
57 // TODO: load xml.xsd in offline mode
58 TOSCADocumentBuilderFactory.logger.error("Schema could not be initalized", e);
59 TOSCADocumentBuilderFactory.logger.debug("We continue nevertheless to enable offline usage");
63 public DocumentBuilder getTOSCADocumentBuilder() {
66 db = this.factory.newDocumentBuilder();
67 } catch (ParserConfigurationException e) {
68 throw new IllegalStateException("document builder could not be initalized", e);