001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.diff;
034:
035: import java.io.*;
036: import java.util.*;
037:
038: import com.jeantessier.classreader.*;
039:
040: public class ListBasedDifferenceStrategy extends
041: DifferenceStrategyDecorator {
042: private Collection allowedElements = new HashSet();
043:
044: public ListBasedDifferenceStrategy(DifferenceStrategy delegate,
045: String filename) throws IOException {
046: super (delegate);
047:
048: load(filename);
049: }
050:
051: public ListBasedDifferenceStrategy(DifferenceStrategy delegate,
052: File file) throws IOException {
053: super (delegate);
054:
055: load(file);
056: }
057:
058: public ListBasedDifferenceStrategy(DifferenceStrategy delegate,
059: BufferedReader in) throws IOException {
060: super (delegate);
061:
062: load(in);
063: }
064:
065: public void load(String filename) throws IOException {
066: BufferedReader in = null;
067:
068: try {
069: in = new BufferedReader(new FileReader(filename));
070: load(in);
071: } finally {
072: if (in != null) {
073: in.close();
074: }
075: }
076: }
077:
078: public void load(File file) throws IOException {
079: BufferedReader in = null;
080:
081: try {
082: in = new BufferedReader(new FileReader(file));
083: load(in);
084: } finally {
085: if (in != null) {
086: in.close();
087: }
088: }
089: }
090:
091: public void load(BufferedReader in) throws IOException {
092: String line;
093: while ((line = in.readLine()) != null) {
094: if (line.length() > 0) {
095: line = line.trim();
096: int pos = line.lastIndexOf(" [");
097: if (pos != -1) {
098: allowedElements.add(line.substring(0, pos));
099: } else {
100: allowedElements.add(line);
101: }
102: }
103: }
104: }
105:
106: public boolean isPackageAllowed(String name) {
107: return isAllowed(name);
108: }
109:
110: public boolean isClassAllowed(String name) {
111: return isAllowed(name);
112: }
113:
114: public boolean isFeatureAllowed(String name) {
115: return isAllowed(name);
116: }
117:
118: public boolean isAllowed(String name) {
119: return allowedElements.contains(name);
120: }
121:
122: public boolean isPackageDifferent(Map oldPackage, Map newPackage) {
123: Map nonEmptyPackage = oldPackage.isEmpty() ? newPackage
124: : oldPackage;
125: String className = (String) nonEmptyPackage.keySet().iterator()
126: .next();
127: String packageName = "";
128: int pos = className.lastIndexOf('.');
129: if (pos != -1) {
130: packageName = className.substring(0, pos);
131: }
132:
133: boolean result = isPackageAllowed(packageName);
134: if (result) {
135: result = super .isPackageDifferent(oldPackage, newPackage);
136: }
137:
138: return result;
139: }
140:
141: public boolean isClassDifferent(Classfile oldClass,
142: Classfile newClass) {
143: Classfile classfile = oldClass != null ? oldClass : newClass;
144:
145: boolean result = isClassAllowed(classfile.getClassName());
146: if (result) {
147: result = super .isClassDifferent(oldClass, newClass);
148: }
149:
150: return result;
151: }
152:
153: public boolean isFieldDifferent(Field_info oldField,
154: Field_info newField) {
155: Field_info field = oldField != null ? oldField : newField;
156:
157: boolean result = isFeatureAllowed(field.getFullSignature());
158: if (result) {
159: result = super .isFieldDifferent(oldField, newField);
160: }
161:
162: return result;
163: }
164:
165: public boolean isMethodDifferent(Method_info oldMethod,
166: Method_info newMethod) {
167: Method_info method = oldMethod != null ? oldMethod : newMethod;
168:
169: boolean result = isFeatureAllowed(method.getFullSignature());
170: if (result) {
171: result = super.isMethodDifferent(oldMethod, newMethod);
172: }
173:
174: return result;
175: }
176: }
|