Merge "Fix build errors in autorelease full clean build"
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.common / src / main / java / org / eclipse / winery / common / TOSCADocumentBuilderFactory.java
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
8  *
9  * Contributors:
10  *     Oliver Kopp - initial API and implementation
11  *******************************************************************************/
12 package org.eclipse.winery.common;
13
14 import java.net.URL;
15
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;
22
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.xml.sax.SAXException;
26
27 /**
28  * Class to produce DocumentBuilders with a pre-loaded TOSCA XSD.
29  * 
30  * In a separate class as TOSCA XSD loading takes a few seconds
31  */
32 public class TOSCADocumentBuilderFactory {
33         
34         private static final Logger logger = LoggerFactory.getLogger(TOSCADocumentBuilderFactory.class);
35         
36         public static final TOSCADocumentBuilderFactory INSTANCE = new TOSCADocumentBuilderFactory();
37         private final DocumentBuilderFactory factory;
38         
39         
40         public TOSCADocumentBuilderFactory() {
41                 this.factory = DocumentBuilderFactory.newInstance();
42                 
43                 this.factory.setNamespaceAware(true);
44                 
45                 // we do not need DTD validation
46                 this.factory.setValidating(false);
47                 
48                 // we do XSD validation
49                 SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
50                 Schema schema;
51                 URL resource = this.getClass().getResource("/TOSCA-v1.0.xsd");
52                 try {
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");
60                 }
61         }
62         
63         public DocumentBuilder getTOSCADocumentBuilder() {
64                 DocumentBuilder db;
65                 try {
66                         db = this.factory.newDocumentBuilder();
67                 } catch (ParserConfigurationException e) {
68                         throw new IllegalStateException("document builder could not be initalized", e);
69                 }
70                 return db;
71         }
72         
73 }