Source code: jpl2/documents/WordDocument.java
1 /***********************************************************************
2 * JavaPsionLink 2.0, a java implementation of the psion link protocol
3 * Copyright (C) 2002, 2003 John S Montgomery (john.montgomery@lineone.net)
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation; either version 2.1 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ************************************************************************/
19 package jpl2.documents;
20
21 import jpl2.common.*;
22 import jpl2.documents.sections.*;
23 import jpl2.convert.*;
24
25
26 import java.io.*;
27
28 import java.awt.Color;
29
30 /** A psion Word document.
31 **/
32 public class WordDocument extends SectionDocument {
33 /** Header section layout (first four bytes). **/
34 public static final int UID1 = 0x10000037;
35 /** File Kind (second four bytes). **/
36 public static final int UID2 = 0x1000006D;
37 /** Application ID (third four bytes). **/
38 public static final int UID3 = 0x1000007F;
39 /** Checksum of other UIDs (fourth four bytes). **/
40 public static final int UID4 = 0x55089FFE;
41
42 private TextSection textSection = null;
43 private PageLayoutSection pageLayoutSection = null;
44 private TextLayoutSection textLayoutSection = null;
45 private WordStylesSection wordStylesSection = null;
46
47 /** Read a Sketch document from the supplied data.
48 **/
49 public WordDocument( Data data ) throws IOException {
50 super( data, UID1, UID2, UID3, UID4 );
51
52 textSection = (TextSection)findSectionFromID( TextSection.ID );
53 pageLayoutSection = (PageLayoutSection)findSectionFromID( PageLayoutSection.ID );
54 textLayoutSection = (TextLayoutSection)findSectionFromID( TextLayoutSection.ID );
55 wordStylesSection = (WordStylesSection)findSectionFromID( WordStylesSection.ID );
56
57 if ( textSection != null && textLayoutSection != null ) {
58 textLayoutSection.setupParagraphs( textSection );
59 }
60
61 }
62
63 /** Replace odd chars with entity codes and other html markup.
64 **/
65 private String toHTML( String text ) {
66 StringBuffer buffer = new StringBuffer();
67 for ( int i = 0; i < text.length(); i++ ) {
68 char c = text.charAt( i );
69 switch( c ) {
70 case 0x06: // new paragraph
71 break;
72 case 0x07: // new line
73 buffer.append( "\n<BR>" );
74 break;
75 case 0x08: // hard page
76 buffer.append( "\n<BR>" );
77 break;
78 case 0x09: // horiz tab
79 // wish there was a nicer way to do this
80 for ( int n = 0; n < 4; n++ )
81 buffer.append( " " );
82 break;
83 case 0x0B: // hard hyphen
84 buffer.append( "-" );
85 break;
86 case 0x0C: // potential hyphen
87 buffer.append( "­" );
88 break;
89 case 0x0E: // object placeholder (maybe sun with link?)
90 break;
91 case 0x0F: // visible space
92 buffer.append( ' ' );
93 break;
94 case 0x10: // hard space
95 buffer.append( " " );
96 break;
97 case '<':
98 buffer.append( "<" );
99 break;
100 case '>':
101 buffer.append( ">" );
102 break;
103 case '&':
104 buffer.append( "&" );
105 break;
106
107 default: {
108 if ( c < 128 )
109 buffer.append( c );
110 else {
111 int n = (int)c; // append the decimal ascii code
112 buffer.append( "&#"+n+";" );
113 }
114 }
115 }
116 }
117 return buffer.toString();
118 }
119
120 private String toHTML( Color color ) {
121 String str = Integer.toHexString( color.getRGB() & 0xFFFFFF );
122 int len = 6-str.length();
123 for ( int i = 0; i < len; i++ ) {
124 str = '0' + str;
125 }
126 return str;
127 }
128
129 public void convertDocument( String fileFormat, OutputStream out ) throws IOException {
130 if ( fileFormat.equals( "txt" ) ) {
131 toText( new OutputStreamWriter( out ) );
132 }
133 else if ( fileFormat.equals( "html" ) ) {
134 toHTML( out );
135 }
136 }
137
138 public void toHTML( OutputStream out ) throws IOException {
139 (new HTMLWordConverter()).convert( this, out );
140 }
141
142 public void toText( Writer w ) throws IOException {
143 w.write( PsionFileConverter.convertPsionString( textSection.getText() ) );
144 w.flush();
145 }
146
147 public String getFolderIndexName( String fileFormat ) {
148 if ( fileFormat.equals( "html" ) )
149 return "index.html";
150
151 return super.getFolderIndexName( fileFormat );
152 }
153
154 public TextSection getTextSection() {
155 return textSection;
156 }
157
158 public PageLayoutSection getPageLayoutSection() {
159 return pageLayoutSection;
160 }
161
162 public TextLayoutSection getTextLayoutSection() {
163 return textLayoutSection;
164 }
165
166 public WordStylesSection getWordStylesSection() {
167 return wordStylesSection;
168 }
169
170 public String toString() {
171 return "WordDocument {\n" +
172 "\tTextSection { " + textSection + " }\n" +
173 "\tTextLayoutSection { " + textLayoutSection + " }\n" +
174 "\tPageLayoutSection { " + pageLayoutSection + " }\n" +
175 "\tAppIDSection { " + getAppIDSection() + " }\n";
176 }
177
178 /*public static void main( String[] args ) {
179 try {
180 //String file = "Word_File.psi";
181 String file = "Welcome to netBook";
182 InputStream in = new BufferedInputStream( new FileInputStream( file ) );
183 Data data = new Data();
184 byte[] buffer = new byte[ 1024 ];
185 int read = -1;
186 while( (read = in.read( buffer )) != -1 ) {
187 System.out.println( read );
188 data.write( buffer, 0, read );
189 }
190 System.out.println( "reading" );
191 data.setOffset( 0 );
192 WordDocument word = new WordDocument( data );
193 System.out.println( word );
194 System.out.println( "done" );
195
196 OutputStream w = new BufferedOutputStream( new FileOutputStream( "word.html" ) );
197 word.toHTML( "word", w );
198 w.flush();
199 w.close();
200
201 }
202 catch( Exception e ) {
203 e.printStackTrace();
204 }
205 }*/
206
207 }