001: /*
002: * Copyright (c) 1998 - 2005 Versant Corporation
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Versant Corporation - initial API and implementation
010: */
011: package com.versant.core.jdo.tools.ant;
012:
013: import java.util.ArrayList;
014: import java.util.List;
015: import java.util.Iterator;
016: import java.io.*;
017:
018: import org.apache.tools.ant.types.*;
019: import org.apache.tools.ant.Project;
020: import org.apache.tools.ant.DirectoryScanner;
021: import com.versant.lib.bcel.classfile.JavaClass;
022: import com.versant.lib.bcel.classfile.ClassParser;
023:
024: /**
025: * @keep-all
026: */
027: public class PCAwareSet extends AbstractFileSet {
028: private ArrayList packages = new ArrayList();
029:
030: public PCAwareSet() {
031: super ();
032: }
033:
034: protected PCAwareSet(PCAwareSet fileset) {
035: super (fileset);
036: }
037:
038: public Package createPackage() {
039: Package pack = new Package();
040: packages.add(pack);
041: return pack;
042: }
043:
044: /**
045: * Return a FileSet that has the same basedir and same patternsets
046: * as this one.
047: */
048: public Object clone() {
049: if (isReference()) {
050: return new PCAwareSet((PCAwareSet) getRef(getProject()));
051: } else {
052: return new PCAwareSet(this );
053: }
054: }
055:
056: public List getAwareClasses(Project project) {
057: ArrayList classlist = new ArrayList();
058: final String dotClass = ".class";
059: StringBuffer buf = new StringBuffer();
060: for (Iterator iterator = packages.iterator(); iterator
061: .hasNext();) {
062: Package s = (Package) iterator.next();
063: String pat = s.getName();
064: if (pat.endsWith("*")) {
065: pat = pat + "*";
066: } else {
067: pat = pat + ".*";
068: }
069: buf.append(pat.replace('.', '/'));
070: buf.append(" ");
071:
072: }
073: setIncludes(buf.toString().trim());
074: DirectoryScanner scanner = getDirectoryScanner(project);
075: String[] incFiles = scanner.getIncludedFiles();
076: for (int j = 0; j < incFiles.length; j++) {
077: String temp = incFiles[j];
078: if (temp.endsWith(dotClass)) {
079: try {
080: File file = new File(scanner.getBasedir(), temp);
081: FileInputStream inputStream = new FileInputStream(
082: file);
083: ClassParser parser = new ClassParser(inputStream,
084: "");
085: JavaClass javaClass = parser.parse();
086: classlist.add(javaClass.getClassName().replace('.',
087: '/')
088: + ".class");
089: } catch (Exception e) {
090: classlist.add(temp);
091: }
092: }
093:
094: }
095: return classlist;
096: }
097:
098: /**
099: * Used to track info about the packages to be
100: */
101: public static class Package {
102: /** The package name */
103: private String name;
104:
105: /**
106: * Set the name of the package
107: *
108: * @param name the package name.
109: */
110: public void setName(String name) {
111: this .name = name.trim();
112: }
113:
114: /**
115: * Get the package name.
116: *
117: * @return the package's name.
118: */
119: public String getName() {
120: return name;
121: }
122:
123: /**
124: * @see java.lang.Object#toString
125: */
126: public String toString() {
127: return getName();
128: }
129: }
130: }
|