1 package gov.nasa.pds.ltdt.gui;
2
3 import gov.nasa.pds.ltdt.gui.configuration.LTDTKeys;
4 import gov.nasa.pds.ltdt.gui.util.DictionaryUtility;
5 import gov.nasa.pds.ltdt.testLabel.TestLabelGenerator;
6 import gov.nasa.pds.ltdt.testLabel.Validator;
7 import gov.nasa.pds.tools.dict.Dictionary;
8 import gov.nasa.pds.tools.dict.parser.UnknownDefinitionException;
9 import gov.nasa.pds.tools.dict.type.InvalidTypeException;
10 import gov.nasa.pds.tools.dict.type.UnsupportedTypeException;
11
12 import java.io.BufferedReader;
13 import java.io.ByteArrayInputStream;
14 import java.io.ByteArrayOutputStream;
15 import java.io.File;
16 import java.io.FileOutputStream;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.io.InputStreamReader;
20 import java.util.List;
21 import java.util.Properties;
22
23 import javax.swing.JOptionPane;
24 import javax.xml.transform.TransformerException;
25
26
27 /***
28 * Validation handling.
29 * @author jwang
30 *
31 */
32 public class Validate {
33
34 /***
35 * Validate a template by generating a temporary file and run through vtool
36 * @param props
37 * @param window
38 */
39 public static void validateTemplate(Properties props, MainWindow window) {
40
41 ByteArrayInputStream in = null;
42 BufferedReader reader = null;
43
44 List dictMergeList = DictionaryUtility.getDictionaryNameList(props.getProperty(LTDTKeys.DICTIONARIES));
45
46 StringBuffer message=new StringBuffer();
47
48 if (window.editorPane.jta.getText().trim().length() >0 ) {
49
50 if (dictMergeList.size()>0 || window.mDictionary!=null) {
51 try {
52
53 File tempfn = File.createTempFile("_test", "tmp");
54
55
56
57 Dictionary vDictionary = window.mDictionary;
58
59 InputStream instream = new ByteArrayInputStream ( window.editorPane.jta.getText().toString().getBytes());
60 TestLabelGenerator testLabel = new TestLabelGenerator();
61 testLabel.createTestLabel(instream, vDictionary, new FileOutputStream(tempfn));
62
63 Validator vd = new Validator();
64 ByteArrayOutputStream out = new ByteArrayOutputStream();
65 vd.doValidation(tempfn.toURL(), vDictionary, out);
66 in = new ByteArrayInputStream(out.toByteArray());
67 reader = new BufferedReader (new InputStreamReader(in));
68
69 tempfn.delete();
70
71 String line =null;
72
73 window.statusPane.jta.setText("");
74 while ( (line=reader.readLine())!=null) {
75 window.statusPane.jta.append(line+System.getProperty("line.separator"));
76 }
77
78
79 }
80 catch (IOException ie) {
81 JOptionPane.showMessageDialog (window, "IO Exception: \n\n"+ie.getMessage());
82 return;
83 }
84
85 catch (InvalidTypeException it) {
86 JOptionPane.showMessageDialog(window, "InvalidType Exception: \n\n"+it.getMessage());
87
88 return;
89 }
90 catch (UnsupportedTypeException ut) {
91 JOptionPane.showMessageDialog(window, "UnsupportedType Exception: \n\n"+ut.getMessage());
92 return;
93 }
94 catch (UnknownDefinitionException ud) {
95 if (message.length()>0) message.delete(0,message.length()-1);
96 message.append("The following elements are not defined:\n\n");
97 message.append(ud.getMessage());
98 message.append("\nPlease import a data dictionary containing the element definitions.\n\n");
99 JOptionPane.showMessageDialog(window, message.toString(), "Missing Element Definitions", JOptionPane.WARNING_MESSAGE);
100
101 return;
102 } catch (TransformerException t) {
103 JOptionPane.showMessageDialog(window, "Transfomer Exception: \n\n" + t.getMessage());
104 return;
105 }
106
107 finally {
108 try {
109 if (reader!=null) reader.close();
110 if (in != null) in.close();
111 }
112 catch (IOException e) { }
113 }
114
115
116 return;
117 }
118
119
120 }
121
122 else {
123 if (message.length()>0) message.delete(0,message.length()-1);
124 message.append("Validation cannot be performed with a blank template.\n");
125 }
126
127 JOptionPane.showMessageDialog(window, message.toString(), "Blank Template", JOptionPane.WARNING_MESSAGE);
128
129 return;
130 }
131 }