001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.types;
019:
020: import org.apache.tools.ant.BuildException;
021: import org.apache.tools.ant.Project;
022:
023: /**
024: * A ZipFileSet is a FileSet with extra attributes useful in the context of
025: * Zip/Jar tasks.
026: *
027: * A ZipFileSet extends FileSets with the ability to extract a subset of the
028: * entries of a Zip file for inclusion in another Zip file. It also includes
029: * a prefix attribute which is prepended to each entry in the output Zip file.
030: *
031: * Since ant 1.6 ZipFileSet can be defined with an id and referenced in packaging tasks
032: *
033: */
034: public class ZipFileSet extends ArchiveFileSet {
035:
036: private String encoding = null;
037:
038: /** Constructor for ZipFileSet */
039: public ZipFileSet() {
040: super ();
041: }
042:
043: /**
044: * Constructor using a fileset arguement.
045: * @param fileset the fileset to use
046: */
047: protected ZipFileSet(FileSet fileset) {
048: super (fileset);
049: }
050:
051: /**
052: * Constructor using a zipfileset arguement.
053: * @param fileset the zipfileset to use
054: */
055: protected ZipFileSet(ZipFileSet fileset) {
056: super (fileset);
057: encoding = fileset.encoding;
058: }
059:
060: /**
061: * Set the encoding used for this ZipFileSet.
062: * @param enc encoding as String.
063: * @since Ant 1.7
064: */
065: public void setEncoding(String enc) {
066: checkZipFileSetAttributesAllowed();
067: this .encoding = enc;
068: }
069:
070: /**
071: * Get the encoding used for this ZipFileSet.
072: * @return String encoding.
073: * @since Ant 1.7
074: */
075: public String getEncoding() {
076: if (isReference()) {
077: AbstractFileSet ref = getRef(getProject());
078: if (ref instanceof ZipFileSet) {
079: return ((ZipFileSet) ref).getEncoding();
080: } else {
081: return null;
082: }
083: }
084: return encoding;
085: }
086:
087: /**
088: * Return a new archive scanner based on this one.
089: * @return a new ZipScanner with the same encoding as this one.
090: */
091: protected ArchiveScanner newArchiveScanner() {
092: ZipScanner zs = new ZipScanner();
093: zs.setEncoding(encoding);
094: return zs;
095: }
096:
097: /**
098: * A ZipFileset accepts another ZipFileSet or a FileSet as reference
099: * FileSets are often used by the war task for the lib attribute
100: * @param p the project to use
101: * @return the abstract fileset instance
102: */
103: protected AbstractFileSet getRef(Project p) {
104: dieOnCircularReference(p);
105: Object o = getRefid().getReferencedObject(p);
106: if (o instanceof ZipFileSet) {
107: return (AbstractFileSet) o;
108: } else if (o instanceof FileSet) {
109: ZipFileSet zfs = new ZipFileSet((FileSet) o);
110: configureFileSet(zfs);
111: return zfs;
112: } else {
113: String msg = getRefid().getRefId()
114: + " doesn\'t denote a zipfileset or a fileset";
115: throw new BuildException(msg);
116: }
117: }
118:
119: /**
120: * Return a ZipFileSet that has the same properties
121: * as this one.
122: * @return the cloned zipFileSet
123: */
124: public Object clone() {
125: if (isReference()) {
126: return ((ZipFileSet) getRef(getProject())).clone();
127: } else {
128: return super .clone();
129: }
130: }
131:
132: /**
133: * A check attributes for zipFileSet.
134: * If there is a reference, and
135: * it is a ZipFileSet, the zip fileset attributes
136: * cannot be used.
137: */
138: private void checkZipFileSetAttributesAllowed() {
139: if (getProject() == null
140: || (isReference() && (getRefid().getReferencedObject(
141: getProject()) instanceof ZipFileSet))) {
142: checkAttributesAllowed();
143: }
144: }
145:
146: }
|