Révision 2881

tmp/org.txm.core/src/java/org/txm/importer/ValidateXml.java (revision 2881)
34 34
import java.io.IOException;
35 35
import java.io.InputStream;
36 36
import java.net.URL;
37
import java.util.regex.Matcher;
38
import java.util.regex.Pattern;
37 39

  
40
import javax.xml.parsers.ParserConfigurationException;
38 41
import javax.xml.stream.XMLInputFactory;
39 42
import javax.xml.stream.XMLStreamConstants;
40 43
import javax.xml.stream.XMLStreamException;
......
42 45

  
43 46
import org.eclipse.osgi.util.NLS;
44 47
import org.txm.core.messages.TXMCoreMessages;
48
import org.txm.utils.logger.Log;
49
import org.xml.sax.SAXException;
45 50

  
46
// TODO: Auto-generated Javadoc
47 51
/**
48
 * Validate a xml file with a stax parser.
52
 * Validate a XML file with a Stax parser.
49 53
 *
50 54
 * @author mdecorde
51 55
 */
52 56
public class ValidateXml {
53
	
57

  
54 58
	/**
55 59
	 * @throws XMLStreamException test a String, not implemented.
56 60
	 *
......
75 79
			XMLInputFactory factory = XMLInputFactory.newInstance();
76 80
			XMLStreamReader parser = factory.createXMLStreamReader(inputData);
77 81
			for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
78
				
82

  
79 83
			}
80 84
			parser.close();
81 85
			inputData.close();
......
87 91
		}
88 92
		return true;
89 93
	}
90
	
94

  
91 95
	/**
92 96
	 * test a content stored in a String, not implemented.
93 97
	 *
......
98 102
	public static boolean test(String xmldata) {
99 103
		return testAndGetError(xmldata).length() == 0;
100 104
	}
101
	
105

  
102 106
	/**
103 107
	 * test a content stored in a String, not implemented.
104 108
	 *
......
108 112
	 */
109 113
	public static String testAndGetError(String xmldata) {
110 114
		try {
111
			Node records = new XmlParser().parseText(xmldata);
115
			new XmlParser().parseText(xmldata);
112 116
			return "";
113 117
		}
114
		catch (Exception e) {
115
			System.out.println(e.getLocalizedMessage());
116
			return e.toString();
118
		catch (IOException | ParserConfigurationException e) {
119
			return e.getMessage();
117 120
		}
121
		catch (SAXException ex) {
122
			return getXMLError(null, ex);
123
		}
118 124
	}
119
	
125

  
126
	private static String getXMLError(String file, Exception e) {
127
		String message = e.getMessage();
128

  
129
		Matcher msgParse = null;
130
		if (message.startsWith("ParseError at")) {
131
			Pattern p = Pattern.compile("(ParseError at \\[row,col\\]:\\[)([0-9]+),([0-9]+)\\]\n" + 
132
					"Message: (.+).");
133
			msgParse = p.matcher(message); // getMessageAndLocation
134
		} else {
135
			Pattern p = Pattern.compile("^(.*) lineNumber: ([0-9]+); columnNumber: ([0-9]+); (.*).$");
136
			msgParse = p.matcher(message); // getMessageAndLocation
137
		}
138

  
139
		if (msgParse.matches() && msgParse.groupCount() == 4) {
140
			String lineNumber = msgParse.group(2);
141
			String colNumber = msgParse.group(3);
142
			String errMsg = msgParse.group(4);
143

  
144
			return TXMCoreMessages.bind("** {0} [line {1}, character {2}]: {3}.", file, lineNumber, colNumber, errMsg);
145
		} else {
146
			return message;
147
		}
148
	}
149

  
120 150
	/**
121 151
	 * test a file.
122 152
	 *
......
143 173
			XMLInputFactory factory = XMLInputFactory.newInstance();
144 174
			XMLStreamReader parser = factory.createXMLStreamReader(inputData);
145 175
			for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
146
				
176

  
147 177
			}
148 178
			parser.close();
149 179
			inputData.close();
150 180
			return true;
151 181
		}
152 182
		catch (Exception e) {
153
			System.out.println(e.getLocalizedMessage());
183
			System.out.println(getXMLError(infile.toString(), e));
154 184
			return false;
155 185
		}
156 186
	}
157
	
187

  
158 188
	/**
159 189
	 * small test to verify a file is a XML-TEI file.
160 190
	 *
......
200 230
			}
201 231
			parser.close();
202 232
			inputData.close();
203
			
233

  
204 234
			boolean test = n == 3 && tei < teiHeader && teiHeader < text;
205 235
			if (test) {
206 236
				return true;
......
228 258
			return false;
229 259
		}
230 260
	}
231
	
261

  
232 262
	/**
233 263
	 * The main method.
234 264
	 *
......
237 267
	 */
238 268
	public static void main(String[] args) {
239 269
		for (File file : new File(System.getProperty("user.home"), "xml/teierrors").listFiles()) {
240
			if (ValidateXml.teiTest(file)) {
270
			if (ValidateXml.test(file)) {
241 271
				System.out.println("OK: " + file); //$NON-NLS-1$
242 272
			}
243 273
			else {
tmp/org.txm.groovy.core/src/groovy/org/txm/scripts/importer/xml/xmlLoader.groovy (revision 2881)
129 129
// copy xml+dtd files
130 130
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
131 131
List<File> srcfiles = srcDir.listFiles();
132
if (srcfiles != null)
133
	for (int i = 0 ; i < srcfiles.size() ; i++) {// check XML format, and copy file into binDir
132
def ignoredFiles = [];
133
if (srcfiles != null) {
134
	for (int i = 0 ; i < srcfiles.size() ; i++) {// check XML format, and copy file into the "txm" directory
134 135
		File f = srcfiles.get(i)
135 136
		if (f.getName().equals("import.xml") || f.getName().matches("metadata\\.....?") || f.getName().endsWith(".properties")) {
136 137
			srcfiles.remove(i);
137 138
			i--;
138
			continue;
139
			continue; // don't raise warnings for those files
139 140
		}
140
		if (ValidateXml.test(f)) {
141
		if (f.getName().toLowerCase().endsWith(".xml") && ValidateXml.test(f)) {
141 142
			FileCopy.copy(f, new File(txmDir, f.getName()));
142 143
		} else {
143
			println "Won't process file "+f;
144
			ignoredFiles << f;
144 145
		}
145 146
	}
147
} else {
148
	println "The $srcDir source directory is empty. Aborting."
149
	return;
150
}
146 151

  
152
if (ignoredFiles.size() > 0) {
153
	println "Warning: some files won't be imported: "+ignoredFiles.join(", ")
154
}
147 155
if (txmDir.listFiles() == null) {
148 156
	println "No txm file to process"
149 157
	return;

Formats disponibles : Unified diff