001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU General
007: * Public License Version 2 only ("GPL") or the Common Development and Distribution
008: * License("CDDL") (collectively, the "License"). You may not use this file except in
009: * compliance with the License. You can obtain a copy of the License at
010: * http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the
011: * License for the specific language governing permissions and limitations under the
012: * License. When distributing the software, include this License Header Notice in
013: * each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun
014: * designates this particular file as subject to the "Classpath" exception as
015: * provided by Sun in the GPL Version 2 section of the License file that
016: * accompanied this code. If applicable, add the following below the License Header,
017: * with the fields enclosed by brackets [] replaced by your own identifying
018: * information: "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Contributor(s):
021: *
022: * The Original Software is NetBeans. The Initial Developer of the Original Software
023: * is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun Microsystems, Inc. All
024: * Rights Reserved.
025: *
026: * If you wish your version of this file to be governed by only the CDDL or only the
027: * GPL Version 2, indicate your decision by adding "[Contributor] elects to include
028: * this software in this distribution under the [CDDL or GPL Version 2] license." If
029: * you do not indicate a single choice of license, a recipient has the option to
030: * distribute your version of this file under either the CDDL, the GPL Version 2 or
031: * to extend the choice of license to its licensees as provided above. However, if
032: * you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then
033: * the option applies only if the new code is made subject to such option by the
034: * copyright holder.
035: */
036:
037: package org.netbeans.installer.utils.helper;
038:
039: import java.io.File;
040: import java.io.FileNotFoundException;
041: import java.io.IOException;
042: import org.netbeans.installer.utils.FileUtils;
043: import org.netbeans.installer.utils.StringUtils;
044:
045: public class FileEntry {
046: private File file;
047: private String name;
048:
049: private boolean metaDataReady;
050:
051: private boolean directory;
052: private boolean empty;
053:
054: private long size;
055: private String md5;
056:
057: private boolean jar;
058: private boolean packed;
059: private boolean signed;
060:
061: private long modified;
062:
063: private int permissions;
064:
065: // constructors /////////////////////////////////////////////////////////////////
066: public FileEntry(final File file) {
067: this .file = file;
068: this .name = file.getAbsolutePath().replace(FileUtils.BACKSLASH,
069: FileUtils.SLASH);
070:
071: this .metaDataReady = false;
072: }
073:
074: public FileEntry(final File file, final boolean empty,
075: final long modified, final int permissions) {
076: this (file);
077:
078: this .directory = true;
079: this .empty = empty;
080: this .modified = modified;
081: this .permissions = permissions;
082:
083: this .metaDataReady = true;
084: }
085:
086: public FileEntry(final File file, final long size,
087: final String md5, final boolean jar, final boolean packed,
088: final boolean signed, final long modified,
089: final int permissions) {
090: this (file);
091:
092: this .directory = false;
093: this .size = size;
094: this .md5 = md5;
095: this .jar = jar;
096: this .packed = packed;
097: this .signed = signed;
098: this .modified = modified;
099: this .permissions = permissions;
100:
101: this .metaDataReady = true;
102: }
103:
104: // getters/setters //////////////////////////////////////////////////////////////
105: public String getName() {
106: return name;
107: }
108:
109: public File getFile() {
110: return file;
111: }
112:
113: public boolean isMetaDataReady() {
114: return metaDataReady;
115: }
116:
117: public boolean isDirectory() {
118: return directory;
119: }
120:
121: public boolean isEmpty() {
122: return empty;
123: }
124:
125: public long getSize() {
126: return size;
127: }
128:
129: public String getMd5() {
130: return md5;
131: }
132:
133: public boolean isJarFile() {
134: return jar;
135: }
136:
137: public boolean isPackedJarFile() {
138: return packed;
139: }
140:
141: public boolean isSignedJarFile() {
142: return signed;
143: }
144:
145: public long getLastModified() {
146: return modified;
147: }
148:
149: public int getPermissions() {
150: return permissions;
151: }
152:
153: // object -> string /////////////////////////////////////////////////////////////
154: public String toString() {
155: if (directory) {
156: return name + StringUtils.LF + directory + StringUtils.LF
157: + empty + StringUtils.LF + modified
158: + StringUtils.LF + Integer.toString(permissions, 8)
159: + StringUtils.LF;
160: } else {
161: return name + StringUtils.LF + directory + StringUtils.LF
162: + size + StringUtils.LF + md5 + StringUtils.LF
163: + jar + StringUtils.LF + packed + StringUtils.LF
164: + signed + StringUtils.LF + modified
165: + StringUtils.LF + Integer.toString(permissions, 8)
166: + StringUtils.LF;
167: }
168: }
169:
170: private String escapeXmlTags(final String str) {
171: return (str == null) ? null : str.replace("&", "&").//NOI18N
172: replace("<", "<").//NOI18N
173: replace(">", ">");//NOI18N
174: }
175:
176: public String toXml() {
177: if (directory) {
178: return "<entry " + "type=\"directory\" " + "empty=\""
179: + empty + "\" " + "modified=\"" + modified + "\" "
180: + "permissions=\""
181: + Integer.toString(permissions, 8) + "\">"
182: + escapeXmlTags(name) + "</entry>";
183: } else {
184: return "<entry " + "type=\"file\" " + "size=\"" + size
185: + "\" " + "md5=\"" + md5 + "\" " + "jar=\"" + jar
186: + "\" " + "packed=\"" + packed + "\" "
187: + "signed=\"" + signed + "\" " + "modified=\""
188: + modified + "\" " + "permissions=\""
189: + Integer.toString(permissions, 8) + "\">"
190: + escapeXmlTags(name) + "</entry>";
191: }
192: }
193:
194: // miscellanea //////////////////////////////////////////////////////////////////
195: public void calculateMetaData() throws IOException {
196: if (file.exists()) {
197: directory = file.isDirectory();
198:
199: if (!directory) {
200: size = file.length();
201: md5 = FileUtils.getMd5(file);
202: jar = FileUtils.isJarFile(file);
203:
204: if (jar) {
205: packed = false; // we cannot determine this
206: signed = FileUtils.isSigned(file);
207: }
208: } else {
209: empty = FileUtils.isEmpty(file);
210: }
211:
212: modified = file.lastModified();
213:
214: metaDataReady = true;
215: } else {
216: throw new FileNotFoundException(file.getAbsolutePath());
217: }
218: }
219: }
|