Source code: com/trapezium/factory/TokenStreamFactory.java
1 /*
2 * @(#)TokenStreamFactory.java
3 *
4 * Copyright (c) 1998 by Trapezium Development LLC. All Rights Reserved.
5 *
6 * The information in this file is the property of Trapezium Development LLC
7 * and may be used only in accordance with the terms of the license
8 * granted by Trapezium.
9 */
10 package com.trapezium.factory;
11
12 import com.trapezium.edit.TokenEditor;
13 import java.io.*;
14 import java.util.Vector;
15 import java.net.URL;
16 import java.util.zip.GZIPInputStream;
17
18 import com.trapezium.util.GlobalProgressIndicator;
19 import com.trapezium.edit.Document;
20 import com.trapezium.util.ProgressIndicator;
21 import com.trapezium.chisel.ChiselSet;
22
23 //
24 // The TokenStreamFactory is a QueuedRequestFactory that creates TokenEnumerators
25 // (which I think should be renamed to be TokenStreams).
26 //
27 // An application should create a single TokenStreamFactory. The Event source has
28 // to be a FactoryResponseListener so that it can be notified of progress of the
29 // factory.
30 //
31 public class TokenStreamFactory extends QueuedRequestFactory {
32
33 public TokenStreamFactory() {
34 super();
35 }
36
37 public String getFactoryName() {
38 return( getFixedFactoryName() );
39 }
40
41 static public String getFixedFactoryName() {
42 return( "Token Stream Factory" );
43 }
44
45
46 // ugly hack to communicate between createTokenStream and handleRequest
47 private File fileSource;
48
49 //
50 // create a TokenStream, when finished, generate an event back to requestor.
51 //
52 public void handleRequest( FactoryData request ) {
53 //System.out.println( "Creating tokens for " + request.getUrl() );
54 try {
55 // check if the request includes a dirty token enumerator.
56 // If it does, this means we have a text-editted token enumerator,
57 // that needs to be re-tokenized.
58 TokenEditor tetest = request.getTokenEditor();
59 if (( tetest != null ) && ( tetest.isDirty() )) {
60 tetest.retokenize();
61 } else {
62 ProgressIndicator pl = ChiselSet.getProgressIndicator( ChiselSet.VALIDATORS );
63 if ( pl != null ) {
64 pl.reset();
65 }
66 Document doc = request.getDocument();
67 if ( pl != null ) {
68 pl.setTitle( "Loading file ... " );
69 }
70 System.out.println( "Loading '" + request.getUrl() + "'" );
71 InputStream is = createTokenStream( request.getUrl(), request );
72 TokenEditor te = new TokenEditor( is, request.getUrl(), pl, fileSource );
73 if ( GlobalProgressIndicator.abortCurrentProcess ) {
74 request.setAborted( true );
75 } else {
76 if ( doc != null ) {
77 doc.setLines( te );
78 doc.setDocumentLoader( request );
79 }
80 request.setFile( fileSource );
81 request.setTokenEditor( te );
82 }
83 }
84 } catch ( java.io.FileNotFoundException fnf ) {
85 System.out.println( "File '" + request.getUrl() + "' not found." );
86 request.setError( fnf );
87 } catch ( Exception e ) {
88 e.printStackTrace();
89 request.setError( e );
90 }
91 }
92
93 InputStream createTokenStream( String url, FactoryData data ) throws FileNotFoundException, IOException {
94
95 fileSource = null;
96
97 URL urlAttempt = null;
98 data.setGzip( false );
99 try {
100 urlAttempt = new URL( url );
101 PushbackInputStream test = new PushbackInputStream( urlAttempt.openStream() );
102 int testchar = test.read();
103 test.unread( testchar );
104 if ( testchar == 0x1f ) {
105 data.setGzip( true );
106 return( new BufferedInputStream( new GZIPInputStream( test )));
107 // } else if ( testchar == 'P' ) {
108 } else {
109 return( new BufferedInputStream( test ));
110 }
111 } catch( Exception e ) {
112 fileSource = new File( url );
113 PushbackInputStream test = new PushbackInputStream( new FileInputStream( fileSource ));
114 int testchar = test.read();
115 test.unread( testchar );
116 if ( testchar == 0x1f ) {
117 data.setGzip( true );
118 return( new BufferedInputStream( new GZIPInputStream( test )));
119 // } else if ( testchar == 'P' ) {
120 } else {
121 return( new BufferedInputStream( test ));
122 }
123 }
124 }
125 }