1
package pipe.dataLayer;
4
import java.io.IOException;
6
import javax.xml.parsers.DocumentBuilder;
7
import javax.xml.parsers.DocumentBuilderFactory;
8
import javax.xml.parsers.ParserConfigurationException;
9
import javax.xml.transform.Transformer;
10
import javax.xml.transform.TransformerException;
11
import javax.xml.transform.TransformerFactory;
12
import javax.xml.transform.stream.StreamResult;
13
import javax.xml.transform.stream.StreamSource;
15
import org.w3c.dom.Document;
16
import org.xml.sax.SAXException;
20
* Create pnmlTransformer object, which deals with getting information from the
21
* XML file, then passes this back to DataLayer constructor for construction
22
* @author Ben Kirby, 10 Feb 2007
24
public class PNMLTransformer {
27
/** Create a Transformer*/
28
public PNMLTransformer() {
34
* Transform a PNML file into a Document which is returned and used to
35
* construct the DataLayer
36
* @returns Document from which DataLayer can be built
37
* @param filename URI location of PNML
39
* @throws SAXException
40
* @throws TransformerException
41
* @throws ParserConfigurationException
43
public Document transformPNML(String filename) {
44
// System.out.println("========================================================");
45
// System.out.println("dataLayer Loading filename=\"" + filename + "\"");
46
// System.out.println("========================================================");
47
File outputObjectArrayList = null;
48
Document document = null;
52
// Create Transformer with XSL Source File
53
StreamSource xsltSource = new StreamSource(
54
Thread.currentThread().getContextClassLoader().getResourceAsStream(
55
"resources/xslt" + System.getProperty("file.separator")
56
+ "GenerateObjectList.xsl"));
57
Transformer transformer = TransformerFactory.newInstance().newTransformer(xsltSource);
59
// TRY TO DO ALL IN MEMORT TO REDUCE READ-WRITE DELAYS
61
// Output for XSLT Transformation
62
outputObjectArrayList = new File(System.getProperty("java.io.tmpdir") +
63
System.getProperty("file.separator") + "ObjectList.xml");
64
outputObjectArrayList.deleteOnExit();
65
StreamSource source = new StreamSource(filename);
66
StreamResult result = new StreamResult(outputObjectArrayList);
67
transformer.transform(source, result);
69
// Get DOM for transformed document
70
document = getDOM(outputObjectArrayList);
71
} catch (IOException e) {
72
System.out.println("IOException thrown in loadPNML(String filename)" +
73
" : dataLayer Class : dataLayer Package");
74
e.printStackTrace(System.err);
75
} catch (SAXException e) {
76
System.out.println("SAXException thrown in loadPNML(String filename)" +
77
" : dataLayer Class : dataLayer Package");
78
e.printStackTrace(System.err);
79
} catch (TransformerException e) {
80
System.out.println("TransformerException thrown in " +
81
"loadPNML(String filename) : dataLayer Class : dataLayer Package");
82
e.printStackTrace(System.err);
83
} catch (ParserConfigurationException e) {
84
System.out.println("ParserConfigurationException thrown in " +
85
"loadPNML(String filename) : dataLayer Class : dataLayer Package");
86
e.printStackTrace(System.err);
89
// Delete transformed file
90
if (outputObjectArrayList != null) {
91
outputObjectArrayList.delete();
95
//BK - surely I want to make everything exact?? - ignore below in favour of catches above
96
// Maxim - make it throw out any exception it gets to the caller. Debugging message left in for now.
97
//} catch (Exception e) {
98
// throw new RuntimeException(e);
104
* Return a DOM for the PNML file at URI pnmlFileName
105
* @param pnmlFileName URI of PNML file
106
* @return A DOM for the PNML file pnmlFileName
107
* @throws ParserConfigurationException
108
* @throws IOException
109
* @throws SAXException
111
public Document getDOM(String pnmlFileName)
112
throws ParserConfigurationException, IOException, SAXException{
113
Document document = null;
116
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
117
documentBuilderFactory.setIgnoringElementContentWhitespace(true);
118
// POSSIBLY ADD VALIDATING
119
// documentBuilderFactory.setValidating(true);
120
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
121
document = documentBuilder.parse(pnmlFileName);
122
} catch (ParserConfigurationException e) {
123
System.err.println("javax.xml.parsers.ParserConfigurationException " +
124
"thrown in getDom(String pnmlFileName) : dataLayer Class : " +
125
"dataLayer Package");
126
} catch (IOException e) {
127
System.err.println("ERROR: File may not be present or have the" +
128
" correct attributes");
129
System.err.println("java.io.IOException thrown in " +
130
"getDom(String pnmlFileName) : dataLayer Class : dataLayer Package");
131
} catch (SAXException e) {
132
System.err.println("org.xml.sax.SAXException thrown in " +
133
"getDom(String pnmlFileName) : dataLayer Class : dataLayer Package");
140
* Return a DOM for the PNML File pnmlFile
141
* @param pnmlFile File Object for PNML of Petri-Net
142
* @return A DOM for the File Object for PNML of Petri-Net
143
* @throws ParserConfigurationException
144
* @throws IOException
145
* @throws SAXException
147
public Document getDOM(File pnmlFile)
148
throws ParserConfigurationException, IOException, SAXException{
149
Document document = null;
152
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
153
documentBuilderFactory.setIgnoringElementContentWhitespace(true);
154
// POSSIBLY ADD VALIDATING
155
// documentBuilderFactory.setValidating(true);
156
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
157
document = documentBuilder.parse(pnmlFile);
158
} catch (ParserConfigurationException e) {
159
System.err.println("javax.xml.parsers.ParserConfigurationException thrown in getDom(String pnmlFileName) : dataLayer Class : dataLayer Package");
160
} catch (IOException e) {
161
System.err.println("ERROR: File may not be present or have the correct attributes");
162
System.err.println("java.io.IOException thrown in getDom(String pnmlFileName) : dataLayer Class : dataLayer Package" + e);
163
} catch (SAXException e) {
164
System.err.println("org.xml.sax.SAXException thrown in getDom(String pnmlFileName) : dataLayer Class : dataLayer Package" + e);
165
System.err.println("Workaround: delete the xmlns attribute from the PNML root node. Probably not ideal, to be fixed when time allows.");