src/main/java/de/unixwork/uwproj/Main.java

changeset 109
1e852be12654
parent 93
df47e083139b
child 112
206e91a8dd18
equal deleted inserted replaced
108:a47b92fab3bd 109:1e852be12654
1 package de.unixwork.uwproj; 1 package de.unixwork.uwproj;
2 2
3 import org.apache.velocity.VelocityContext; 3 import org.apache.velocity.VelocityContext;
4 import org.apache.velocity.app.VelocityEngine; 4 import org.apache.velocity.app.VelocityEngine;
5 5 import org.xml.sax.SAXException;
6
7 import javax.xml.XMLConstants;
8 import javax.xml.parsers.DocumentBuilderFactory;
9 import javax.xml.transform.dom.DOMSource;
10 import javax.xml.transform.stream.StreamSource;
11 import javax.xml.validation.SchemaFactory;
6 import java.io.*; 12 import java.io.*;
7 import java.nio.file.Files; 13 import java.nio.file.Files;
8 import java.nio.file.Path; 14 import java.nio.file.Path;
9 import java.nio.file.Paths; 15 import java.nio.file.Paths;
10 import java.nio.file.StandardCopyOption; 16 import java.nio.file.StandardCopyOption;
17 public final static String IN_FILE_DEFAULT = "make/project.xml"; 23 public final static String IN_FILE_DEFAULT = "make/project.xml";
18 public final static String TPL_FILE_DEFAULT = "make/configure.vm"; 24 public final static String TPL_FILE_DEFAULT = "make/configure.vm";
19 public final static String OUT_FILE_DEFAULT = "configure"; 25 public final static String OUT_FILE_DEFAULT = "configure";
20 26
21 static Project loadProjectFile(String fileName) { 27 static Project loadProjectFile(String fileName) {
22 try { 28 try (final var xsdResource = Objects.requireNonNull(Main.class.getClassLoader().getResourceAsStream("make/uwproj.xsd"))) {
23 return new Project(new File(fileName)); 29 // Create the XSD validator
30 final var factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
31 final var schema = factory.newSchema(new StreamSource(xsdResource));
32 final var validator = schema.newValidator();
33
34 // Load the DOM from the input file
35 final var dom = DocumentBuilderFactory.
36 newDefaultNSInstance().
37 newDocumentBuilder().
38 parse(fileName).
39 getDocumentElement();
40
41 // Validate the XML
42 validator.validate(new DOMSource(dom));
43
44 // Parse the XML
45 return new Project(dom);
46 } catch (SAXException saxerror) {
47 System.err.println(saxerror.getMessage());
48 throw new RuntimeException("XML validation failed.");
24 } catch (Exception e) { 49 } catch (Exception e) {
25 throw new RuntimeException(e); 50 throw new RuntimeException(e);
26 } 51 }
27 } 52 }
28 53

mercurial