1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package org.openoffice.xmerge.converter.palm;
25 
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.ByteArrayOutputStream;
29 
30 import java.io.OutputStream;
31 import java.io.UnsupportedEncodingException;
32 
33 import org.openoffice.xmerge.Document;
34 
35 /**
36  *  <p> A <code>PalmDocument</code> is palm implementation of the
37  *  <code>Document</code> interface.</p>
38  *
39  *  <p>This implementation allows the Palm device format to be
40  *  read via an <code>InputStream</code> and written via an
41  *  <code>OutputStream</code>.</p>
42  *
43  *  @author  Martin Maher
44  */
45 
46 public class PalmDocument
47     implements Document {
48 
49     /**
50      *  The internal representation of a pdb.
51      */
52     private PalmDB pdb;
53 
54     /**
55      *  The file name.
56      */
57     private String fileName;
58 
59     /**
60      *  Constructor to create a <code>PalmDocument</code>
61      *  from an <code>InputStream</code>.
62      *
63      *  @param  is  <code>InputStream</code> containing a PDB.
64      *
65      *  @throws  IOException  If any I/O error occurs.
66      */
PalmDocument(InputStream is)67     public PalmDocument(InputStream is) throws IOException {
68         read(is);
69     }
70 
71 
72     /**
73      *  Constructor to create a <code>PalmDocument</code> with
74      *  <code>Record</code> objects.  <code>recs.length</code>
75      *  can be zero for an empty PDB.
76      *
77      *  @param  name       Suggested PDB name in <code>String</code>.
78      *  @param  creatorID  The PDB Creator ID.
79      *  @param  typeID     The PDB Type ID.
80      *  @param  version    The PDB header version.
81      *  @param  attribute  The PDB header attribute.
82      *  @param  recs       Array of <code>Record</code> objects.
83      *
84      *  @throws  NullPointerException  If <code>recs</code> is null.
85      */
PalmDocument(String name, int creatorID, int typeID, int version, short attribute, Record[] recs)86     public PalmDocument(String name, int creatorID, int typeID, int version,
87         short attribute, Record[] recs)
88         throws UnsupportedEncodingException {
89         pdb = new PalmDB(name, creatorID, typeID, version, attribute, recs);
90         fileName = pdb.getPDBNameString();
91     }
92 
93 
94     /**
95      *  Reads in a file from the <code>InputStream</code>.
96      *
97      *  @param  is  <code>InputStream</code> to read in its content.
98      *
99      *  @throws  IOException  If any I/O error occurs.
100      */
101 
read(InputStream is)102     public void read(InputStream is) throws IOException {
103         PdbDecoder decoder = new PdbDecoder();
104 	ByteArrayOutputStream baos = new ByteArrayOutputStream();
105         byte[] buf = new byte[4096];
106         int n = 0;
107 	while ((n = is.read(buf)) > 0) {
108 		baos.write(buf, 0, n);
109         }
110         byte[] bytearr = baos.toByteArray();
111         pdb = decoder.parse(bytearr);
112         fileName = pdb.getPDBNameString();
113     }
114 
115 
116     /**
117      *  Writes the <code>PalmDocument</code> to an <code>OutputStream</code>.
118      *
119      *  @param  os  The <code>OutputStream</code> to write the content.
120      *
121      *  @throws  IOException  If any I/O error occurs.
122      */
write(OutputStream os)123     public void write(OutputStream os) throws IOException {
124         PdbEncoder encoder = new PdbEncoder(pdb);
125         encoder.write(os);
126     }
127 
128 
129     /**
130      *  Returns the <code>PalmDB</code> contained in this object.
131      *
132      *  @return  The <code>PalmDB</code>.
133      */
getPdb()134     public PalmDB getPdb() {
135         return pdb;
136     }
137 
138 
139     /**
140      *  Sets the <code>PalmDocument</code> to a new <code>PalmDB</code>
141      *  value.
142      *
143      *  @param  pdb  The new <code>PalmDB</code> value.
144      */
setPdb(PalmDB pdb)145     public void setPdb(PalmDB pdb) {
146         this.pdb = pdb;
147 
148         String name = pdb.getPDBNameString();
149         fileName = name;
150     }
151 
152 
153     /**
154      *  Returns the name of the file.
155      *
156      *  @return  The name of the file represented in the
157      *           <code>PalmDocument</code>.
158      */
getFileName()159     public String getFileName() {
160         return fileName + ".pdb";
161     }
162 
163 
164     /**
165      *  Returns the <code>Document</code> name.
166      *
167      *  @return  The <code>Document</code> name.
168      */
getName()169     public String getName() {
170         return fileName;
171     }
172 }
173