source: gli/trunk/src/org/greenstone/gatherer/collection/CollectionTreeNode.java@ 16770

Last change on this file since 16770 was 16770, checked in by ak19, 16 years ago

Removed debug statement

  • Property svn:keywords set to Author Date Id Revision
File size: 6.2 KB
Line 
1/**
2 *############################################################################
3 * A component of the Greenstone Librarian Interface, part of the Greenstone
4 * digital library suite from the New Zealand Digital Library Project at the
5 * University of Waikato, New Zealand.
6 *
7 * Author: Michael Dewsnip, NZDL Project, University of Waikato, NZ
8 *
9 * Copyright (C) 2004 New Zealand Digital Library Project
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *############################################################################
25 */
26
27package org.greenstone.gatherer.collection;
28
29
30import java.io.*;
31import javax.swing.*;
32import org.greenstone.gatherer.cdm.CollectionDesignManager;
33import org.greenstone.gatherer.file.FileNode;
34import org.greenstone.gatherer.util.JarTools;
35import java.util.Set;
36import java.util.Iterator;
37import java.nio.charset.Charset;
38
39
40/** This class represents one node in the collection tree. */
41public class CollectionTreeNode
42 extends FileNode
43{
44 static final public ImageIcon GREEN_FILE_ICON = JarTools.getImage("greenfile.gif", true);
45
46 /** Is this file a metadata database that is explodable with the explode_metadata_databases.pl script? */
47 private boolean is_explodable = false;
48
49 /** Keeps track of whether this file can be replaced with a Greenstone-generated html file.
50 * To work with replace_srcdoc_with_html.pl */
51 private boolean is_srcreplaceable = false;
52
53 private String displayFileName = null;
54
55 public CollectionTreeNode(File file)
56 {
57 super(file);
58
59 this.is_explodable = CollectionDesignManager.plugin_manager.isFileExplodable(file);
60 // To work with replace_srcdoc_with_html.pl
61 this.is_srcreplaceable = CollectionDesignManager.plugin_manager.isFileSrcReplaceable(file);
62
63 displayFileName = calcDisplayString();
64 }
65
66 /** Similar to calcDisplayString, but this version only checks whether the filename
67 * String is UTF8. If so, it converts it into UTF8 and returns it. If the filename
68 * is not UTF8, it is returned unchanged.
69 */
70 protected String quickCalcDisplayString() {
71 String filename = super.toString();
72 try{
73 String utf8filename = new String(filename.getBytes(), "UTF8");
74 if(utf8filename.indexOf('\ufffd') == -1) {
75 return utf8filename;
76 } else { // contains the character indicating that it's invalid utf8
77 // return the original string
78 return filename;
79 }
80 } catch(java.io.UnsupportedEncodingException e) {
81 return filename;
82 }
83 }
84
85 /** This method returns a string representation of the filenodes in the Collection
86 * Tree, that can then be displayed in the tree.
87 * We'll initially assume that the filenames are utf8 encoded and so convert the
88 * filename into utf8 for proper presentation in the Collection tree pane.
89 * If the filenames are not utf8, then the conversion would have introduced funny
90 * characters. Therefore, when converting to utf8, if the converted filename
91 * contains the special character '\ufffd', then we know the conversion did not work
92 * and we return the original string which may or may not be properly presented by
93 * default.
94 * See http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/CharsetDecoder.html
95 * which says "How a decoding error is handled depends upon the action requested for
96 * that type of error, which is described by an instance of the CodingErrorAction class.
97 * The possible error actions are to ignore the erroneous input, report the error to
98 * the invoker via the returned CoderResult object, or replace the erroneous input with
99 * the current value of the replacement string. The replacement has the initial value
100 * "\uFFFD"; its value may be changed via the replaceWith method."
101 * The following made me think that String(byte[], String charsetName) constructor may
102 * use the replacement value \uFFFD.
103 * http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20512969.html
104 * mentions the following which made me think of this:
105 * convertedStr = convertedStr.replace('\ufffd', ' ');
106 */
107 protected String calcDisplayString() {
108 String filename = super.toString();
109
110 try{
111 String[] charsets = {"UTF-8", "ISO-8859-1", "US-ASCII", "UTF-16BE", "UTF-16LE", "UTF-16"};
112 for(int i = 0; i < charsets.length; i++) {
113 String charset = charsets[i];
114 String convertedfilename = new String(filename.getBytes(), charset);
115 if(convertedfilename.indexOf('\ufffd') != -1) {
116 // encountered the character that indicates when the conversion is invalid
117 convertedfilename = null;
118 continue; // try to use the next charset to encode the filename as
119 }
120 else { // valid conversion
121 return convertedfilename;
122 }
123 }
124 } catch(java.io.UnsupportedEncodingException e) {
125 return filename;
126 }
127
128 return filename; // attempted conversions all failed
129 }
130
131
132 public FileNode addChildNode(File file)
133 {
134 CollectionTreeNode child_node = new CollectionTreeNode(file);
135 child_node.setModel(model);
136 child_node.setParent(this);
137 return child_node;
138 }
139
140
141 public boolean isExplodable()
142 {
143 return is_explodable;
144 }
145
146 // To work with replace_srcdoc_with_html.pl
147 public boolean isSrcReplaceable()
148 {
149 return is_srcreplaceable;
150 }
151
152 /** This method returns a string representation of the filenodes in the Collection
153 * Tree, which is what will be displayed in the tree. It tries to convert it into
154 * some common encoding formats. Failing that, the unchanged filepath is returned.
155 * @see displayString
156 */
157 public String toString()
158 {
159 if(displayFileName == null) {
160 displayFileName = calcDisplayString();
161 }
162 return displayFileName;
163 }
164}
Note: See TracBrowser for help on using the repository browser.