package com.xmlit.project.engine.test;
import java1.util.regex.Matcher;
import java1.util.regex.Pattern;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSSerializer;
import com.xmlit.project.engine.marshal.UnmarshalImpl;
import com.xmlit.project.engine.struct.StructChoice;
import com.xmlit.project.engine.struct.StructSequence;
import com.xmlit.project.engine.struct.StructSimple;
import com.xmlit.project.engine.struct.impl.CalcContext;
import com.xmlit.project.engine.struct.impl.StructChoiceImpl;
import com.xmlit.project.engine.struct.impl.StructSequenceImpl;
import com.xmlit.project.engine.struct.impl.StructSimpleImpl;
public class Test {
static String msg = "xxxxxx1111144444444455555555\r\nxxxxxxxxYYYYYYYYY\r\nxxxxxx1111144444444455555555\r\nxxxxxxxxYYYYYYYYY\r\nxxxxxxxxxxxxyyyyy";
public static void main1(String[] args) throws ClassCastException, ClassNotFoundException, InstantiationException, IllegalAccessException {
StructSequence root = new StructSequenceImpl("root");
root.setDelimiter("\r\n");
StructSequenceImpl ab = new StructSequenceImpl("ab");
ab.setDelimiter("\r\n");
ab.setMinOccurrences(1).setMaxOccurrences(2);
StructSequence a = new StructSequenceImpl("a");
StructSequence b = new StructSequenceImpl("b");
StructSequence c = new StructSequenceImpl("c");
root.addChild(ab).addChild(c);
ab.addChild(a).addChild(b);
StructSimple x1 = new StructSimpleImpl("x1").setSimplePattern("[a-z]{6,9}");
StructSimple y1 = new StructSimpleImpl("y1").setSimplePattern("[0-9]{5,9}");
y1.setMaxOccurrences(10).setMinOccurrences(2);
a.addChild(x1).addChild(y1);
StructSimple x2 = new StructSimpleImpl("x2").setSimplePattern("[a-z]{6,9}");
StructSimple y2 = new StructSimpleImpl("y2").setSimplePattern("[A-Z]{5,9}");
b.addChild(x2).addChild(y2);
StructSimple n = new StructSimpleImpl("n").setSimplePattern("[a-z]{6,9}");
StructSimple m = new StructSimpleImpl("m").setSimplePattern("[a-z]{5,9}");
c.addChild(n).addChild(m);
// n.setMinOccurrences(0);
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS impl =
(DOMImplementationLS)registry.getDOMImplementation("LS");
long time = System.currentTimeMillis();
new UnmarshalImpl().unmarshal(msg, root);
System.out.println("time="+(System.currentTimeMillis()-time));
LSSerializer writer = impl.createLSSerializer();
writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
String str = writer.writeToString(new UnmarshalImpl().unmarshal(msg, root));
System.out.println(str);
/*String reg = root.calcRegex(new CalcContext());
System.out.println("---->"+reg);
Pattern datePatt = Pattern.compile(reg);
Matcher m1 = datePatt.matcher(msg);
if (m1.matches()) {
for (int i = 0;i <= m1.groupCount();i++) {
System.out.println(i +"============================");
System.out.println(m1.group(i));
}
}
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
Element e = doc.createElement("www");
e.appendChild(doc.createElement("rrrr"));
System.out.println("-------------"+e.getChildNodes().item(0));
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
}
public static void main(String[] args) throws ClassCastException, ClassNotFoundException, InstantiationException, IllegalAccessException {
StructSequence root = new StructSequenceImpl("root");
root.setDelimiter("/");
StructSimple x1 = (StructSimple) new StructSimpleImpl("a").setSimplePattern("aaa").setMinOccurrences(1).setMaxOccurrences(1);
StructSimple y1 = (StructSimple) new StructSimpleImpl("b").setSimplePattern("bbb").setMinOccurrences(1).setMaxOccurrences(1);
root.addChild(x1).addChild(y1);
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS impl =
(DOMImplementationLS)registry.getDOMImplementation("LS");
// long time = System.currentTimeMillis();
// new UnmarshalImpl().unmarshal("aaa/bbb", root);
// System.out.println("time="+(System.currentTimeMillis()-time));
LSSerializer writer = impl.createLSSerializer();
writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
String str = writer.writeToString(new UnmarshalImpl().unmarshal("aaa/bbb", root));
System.out.println(str);
/*String reg = root.calcRegex(new CalcContext());
System.out.println("---->"+reg);
Pattern datePatt = Pattern.compile(reg);
Matcher m1 = datePatt.matcher(msg);
if (m1.matches()) {
for (int i = 0;i <= m1.groupCount();i++) {
System.out.println(i +"============================");
System.out.println(m1.group(i));
}
}
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
Element e = doc.createElement("www");
e.appendChild(doc.createElement("rrrr"));
System.out.println("-------------"+e.getChildNodes().item(0));
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
}
}