package com.xmlit.project.engine.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.w3c.dom.Document;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSSerializer;
import org.xml.sax.SAXException;
import com.xmlit.project.engine.kxsd.KXSDReader;
import com.xmlit.project.engine.marshal.MarshalImpl;
import com.xmlit.project.engine.marshal.UnmarshalImpl;
import com.xmlit.project.engine.struct.Struct;
public class MultiMessageTest {
public static void main(String[] args) throws Exception{
File [] messages = new File("E:/kmaster/for_marc_new/formatting_xsd/message_samples/").listFiles();
for (int i = 0;i < messages.length;i++) {
//if (i >1) return;
String pref = messages[i].getName().substring(0, 3);
String xsd = file2String(new File("C:/formats/fin."+pref+".2008.xsd"));
//if (!xsd.equals("C:/formats/fin.700.2008.xsd")) continue;
System.out.println(messages[i]);
KXSDReader reader = new KXSDReader();
Struct root=null;
try {
root = reader.readSchema(xsd);
} catch (Throwable e1) {
e1.printStackTrace();
System.out.println("##########");
continue;
}
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS impl =
(DOMImplementationLS)registry.getDOMImplementation("LS");
LSSerializer writer = impl.createLSSerializer();
writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
try {
String message = file2String(messages[i]);
Document doc;
try {
doc = new UnmarshalImpl().unmarshal(message, root);
String msg = new MarshalImpl().marshal(doc, root);
System.out.println("dif=" + (message.equals(msg)));
if (!message.equals(msg)) {
System.out.println(writer.writeToString(doc));
System.out.println("++++++++++++BEFORE");
System.out.println(message);
System.out.println("++++++++++++AFTER");
System.out.println(msg);
System.out.println("-------------------------------");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
continue;
}
/* long time = System.currentTimeMillis();
for (int j=0;j < 1000;j++)
new UnmarshalImpl().unmarshal(message, root);
System.out.println("time="+(System.currentTimeMillis()-time));
*/
// Document docMsg = new UnmarshalImpl().unmarshal(file2String(messages[i]), root);
// str = writer.writeToString(docMsg);
// System.out.println("---->"+validate(docMsg, new File(xsd)));
} catch (Throwable e) {
// TODO Auto-generated catch block
//e.printStackTrace();
throw new RuntimeException(e);
}
//System.out.println(str);
}
}
public static String file2String(File file) {
StringBuffer result = new StringBuffer();
try {
FileReader is = new FileReader(file);
BufferedReader br = new BufferedReader(is);
String tmpBuf = null;
while ((tmpBuf = br.readLine()) != null) {
result.append(tmpBuf);
if (!"".equals(tmpBuf))
result.append("\r\n");
}
return result.toString().substring(0, result.length() - 2);
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
private synchronized static Validator getValidator(File XSDfile) {
Validator validator = files2XSDValidators.get(XSDfile);
if (validator == null) {
// create a SchemaFactory capable of understanding XML schemas
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// load an XML schema
Schema schema = null;
try {
Source schemaFile = new StreamSource(new FileReader(XSDfile));
schema = factory.newSchema(schemaFile);
}
catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
catch (SAXException e) {
throw new RuntimeException(e);
}
// create a Validator instance and validate document
validator = schema.newValidator();
files2XSDValidators.put(XSDfile, validator);
}
return validator;
}
private static String validate(Document xml, File XSDfile) {
Validator validator = getValidator(XSDfile);
String result = null;
try {
validator.validate(new DOMSource(xml));
}
catch (SAXException e) {
result = e.getMessage();
}
catch (IOException e) {
throw new RuntimeException(e);
}
return result;
}
private static HashMap<File, Validator> files2XSDValidators = new HashMap<File, Validator>();
}