aboutsummaryrefslogtreecommitdiff
path: root/TestIt.java
blob: 74088d159d37e963b3c7e9a81980a0776f4b0a00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
class TestIt
{	
	public static void main( String[] args )
	{
		GrokHtml foo = new GrokHtml();
		long m1 = 0, m2 = 0, doc1 = 0, doc2 = 0;
		String expr;

		/* Parse an easy expression */
		expr = "tr -> td -> text:\"foo = (.*)\"";
		try
		{
			m1 = foo.ParseExpression( expr );
			System.out.println( "Expression 1 parsed correctly" );
		}
		catch( java.text.ParseException e )
		{
			System.out.println( "Error parsing expression 1: " + e.getMessage( ) + "\n" + expr );
			int i, off = e.getErrorOffset( );
			for( i = 0; i < off; i++ )
				System.out.print( " " );
			System.out.println( "^" );
		}

		/* Parse another easy expression */
		expr = "tr -> td -> text:\"bar = (.*)\"";
		try
		{
			m2 = foo.ParseExpression( expr );
			System.out.println( "Expression 2 parsed correctly" );
		}
		catch( java.text.ParseException e )
		{
			System.out.println( "Error parsing expression 2: " + e.getMessage( ) + "\n" + expr );
			int i, off = e.getErrorOffset( );
			for( i = 0; i < off; i++ )
				System.out.print( " " );
			System.out.println( "^" );
		}

		/* Parse a broken expression */
		expr = "table<border=\"0\" foo=>";
		try
		{
			long m = foo.ParseExpression( expr );
			System.out.println( "This expression should not parse correctly" );
		}
		catch( java.text.ParseException e )
		{
			System.out.println( "Error parsing broken expression: " + e.getMessage( ) + "\n" + expr );
			int i, off = e.getErrorOffset( );
			for( i = 0; i < off; i++ )
				System.out.print( " " );
			System.out.println( "^" );
		}

		/* Open a document from a byte[] */
		try
		{
			String testdoc = "<h1>Foo!</h1><h2>Bar!</h2><table><tr><td>foo = baz</td></tr></table>";
			doc1 = foo.OpenDocumentFromBytes( testdoc.getBytes( "US-ASCII" ));
			System.out.println( "Document opened successfully from a string" );
		}
		catch( java.lang.RuntimeException e )
		{
			System.out.println( e.getMessage( ));
		}
		catch( java.io.UnsupportedEncodingException e )
		{
			System.out.println( e.getMessage( ));
		}

		/* Open a document from a URI */
		try
		{
			doc2 = foo.OpenDocumentFromURI( "test.html" );
			System.out.println( "Document opened successfully from a URI" );
		}
		catch( java.lang.RuntimeException e )
		{
			System.out.println( e.getMessage( ));
		}

		/* Try both machines on doc 1 */
		if( doc1 != 0 )
		{
			if( m1 != 0 )
			{
				try
				{
					String result = foo.SearchDocument( doc1, "foo is \\1", m1 );
					System.out.println( "Expression 1 on doc 1: " + result );
				}
				catch( java.lang.RuntimeException e )
				{
					System.out.println( e.getMessage( ));
				}
				catch( java.lang.OutOfMemoryError e )
				{
					System.out.println( e.getMessage( ));
				}
			}

			if( m2 != 0 )
			{
				try
				{
					String result = foo.SearchDocument( doc1, "bar is \\1", m2 );
					System.out.println( "Expression 2 on doc 1: " + result );
				}
				catch( java.lang.RuntimeException e )
				{
					System.out.println( "This error is ok: " + e.getMessage( ));
				}
				catch( java.lang.OutOfMemoryError e )
				{
					System.out.println( e.getMessage( ));
				}
			}

			/* Free it now that we're finished */
			foo.FreeDocument( doc1 );
		}

		/* Try both machines on doc 2 */
		if( doc2 != 0 )
		{
			if( m1 != 0 )
			{
				try
				{
					String result = foo.SearchDocument( doc2, "foo is \\1", m1 );
					System.out.println( "Expression 1 on doc 2: " + result );
				}
				catch( java.lang.RuntimeException e )
				{
					System.out.println( e.getMessage( ));
				}
				catch( java.lang.OutOfMemoryError e )
				{
					System.out.println( e.getMessage( ));
				}
			}

			if( m2 != 0 )
			{
				try
				{
					String result = foo.SearchDocument( doc2, "bar is \\1", m2 );
					System.out.println( "Expression 2 on doc 2: " + result );
				}
				catch( java.lang.RuntimeException e )
				{
					System.out.println( e.getMessage( ));
				}
				catch( java.lang.OutOfMemoryError e )
				{
					System.out.println( e.getMessage( ));
				}
			}

			/* Free it now that we're finished */
			foo.FreeDocument( doc2 );
		}

		/* Free the machines */
		if( m1 != 0 )
			foo.FreeMachine( m1 );
		if( m2 != 0 )
			foo.FreeMachine( m2 );
	}	
}