~tapaal-contributor/tapaal/disappearing-tokens-1940098

« back to all changes in this revision

Viewing changes to src/pipe/dataLayer/PNMLTransformer.java

  • Committer: Kenneth Yrke Jørgensen
  • Date: 2011-04-12 09:50:16 UTC
  • mfrom: (329.1.188 tapaal-1.5)
  • Revision ID: mail@yrke.dk-20110412095016-e4hqdgab5596ja09
Merged with branch addning support for new 1.5 features

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package pipe.dataLayer;
2
 
 
3
 
import java.io.File;
4
 
import java.io.IOException;
5
 
 
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;
14
 
 
15
 
import org.w3c.dom.Document;
16
 
import org.xml.sax.SAXException;
17
 
 
18
 
 
19
 
/**
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
23
 
 */
24
 
public class PNMLTransformer {
25
 
   
26
 
   
27
 
   /** Create a Transformer*/
28
 
   public PNMLTransformer() {
29
 
      ;
30
 
   }
31
 
   
32
 
   
33
 
   /** 
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
38
 
    * @throws IOException
39
 
    * @throws SAXException
40
 
    * @throws TransformerException
41
 
    * @throws ParserConfigurationException
42
 
    */
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;
49
 
      
50
 
 
51
 
      try {
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);
58
 
         
59
 
         // TRY TO DO ALL IN MEMORT TO REDUCE READ-WRITE DELAYS
60
 
         
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);
68
 
         
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);
87
 
      }
88
 
      
89
 
//              Delete transformed file
90
 
      if (outputObjectArrayList != null) {
91
 
         outputObjectArrayList.delete();
92
 
      }
93
 
      return document;
94
 
      
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);
99
 
      //}           
100
 
   }
101
 
 
102
 
   
103
 
   /**
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
110
 
    */
111
 
   public Document getDOM(String pnmlFileName)
112
 
           throws ParserConfigurationException, IOException, SAXException{
113
 
      Document document = null;
114
 
      
115
 
      try {
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");
134
 
      }
135
 
      return document;
136
 
   }
137
 
 
138
 
   
139
 
   /**
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
146
 
    */
147
 
   public Document getDOM(File pnmlFile)
148
 
           throws ParserConfigurationException, IOException, SAXException{
149
 
      Document document = null;
150
 
      
151
 
      try {
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.");
166
 
      }
167
 
      return document;
168
 
   }
169
 
 
170
 
}