package com.ricebridge.xmlman.in.test;
import com.ricebridge.xmlman.*;
import com.ricebridge.xmlman.in.*;
import com.ricebridge.xmlman.tp.BaseXPath;
import com.ricebridge.xmlman.tp.expr.Expr;
import org.jostraca.util.*;
import junit.framework.*;
import junit.textui.*;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
import java.util.*;
import java.io.*;
public class SubPathManagerTest extends TestCase {
public SubPathManagerTest( String pName ) {
super( pName );
}
public static TestSuite suite() {
return new TestSuite( SubPathManagerTest.class );
}
public static void main( String[] pArgs ) {
TestRunner.run( suite() );
}
public void testSanity() throws Exception {
DocContext dc = new DocContext( new XmlSpec() );
StringArrayRecordListener rt = new StringArrayRecordListener();
ExprWalker ew = TestUtil.makeExprWalker();
String recordpath = "/library/store/book";
List recordpl = ew.makePaths( makeExpr( recordpath ), true );
TargetPath rectp = (TargetPath)recordpl.get(0);
rectp.activate( 1, dc );
rectp.setDefaultActivation( rectp.active(dc) );
String fieldpath = "@name";
ew = TestUtil.makeExprWalker();
List fpl = null;
fpl = ew.makePaths( makeExpr( fieldpath ), true );
TargetPath fp = (TargetPath) fpl.get(0);
fp.deactivate(dc);
rectp.addSubPath( fp );
recordpl.add( fp );
dc.setExpression( "book", new DirectExpression( fp ) );
fieldpath = "publisher";
ew = TestUtil.makeExprWalker();
fpl = ew.makePaths( makeExpr( fieldpath ), true );
fp = (TargetPath) fpl.get(0);
fp.deactivate(dc);
rectp.addSubPath( fp );
recordpl.add( fp );
dc.setExpression( "publisher", new DirectExpression( fp ) );
fieldpath = "copyright/@year";
ew = TestUtil.makeExprWalker();
fpl = ew.makePaths( makeExpr( fieldpath ), true );
fp = (TargetPath) fpl.get(0);
fp.deactivate(dc);
rectp.addSubPath( fp );
recordpl.add( fp );
dc.setExpression( "copyright", new DirectExpression( fp ) );
ew = TestUtil.makeExprWalker();
fpl = ew.makePaths( makeExpr( "/library/@name" ), true );
fp = (TargetPath) fpl.get(0);
fp.activate( 1, dc );
fp.setDefaultActivation( fp.active(dc) );
recordpl.add( fp );
dc.setExpression( "library", new DirectExpression( fp ) );
System.out.println( recordpl );
System.out.println( dc );
PointHandler bph = new PointHandler( recordpl, dc );
SaxHandler sh = new SaxHandler( true );
sh.setPointHandler( bph );
System.out.println( "PARSE" );
parse( sh, "com/ricebridge/xmlman/test/sanity.xml" );
List rows = rt.getRecords();
System.out.println( "RECORDS:\n"+TestUtil.dataToString(rows) );
}
public Expr makeExpr( String pXPath ) throws Exception {
BaseXPath bxp = new BaseXPath(pXPath,null);
Expr expr = bxp.getRootExpr();
expr = expr.simplify();
return expr;
}
public void parse( SaxHandler pSaxHandler, String pTextXmlPath ) throws Exception {
File xmlFile = TestUtil.findFile( pTextXmlPath );
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
saxParser.parse( xmlFile, pSaxHandler );
}
}