001: /***
002: * Retrotranslator: a Java bytecode transformer that translates Java classes
003: * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
004: *
005: * Copyright (c) 2005 - 2008 Taras Puchko
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * 3. Neither the name of the copyright holders nor the names of its
017: * contributors may be used to endorse or promote products derived from
018: * this software without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
022: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
023: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
024: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
025: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
026: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
027: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
028: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
029: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
030: * THE POSSIBILITY OF SUCH DAMAGE.
031: */package net.sf.retrotranslator.transformer;
032:
033: /**
034: * @author Taras Puchko
035: */
036: class FileTranslator {
037:
038: private final ClassTransformer classTransformer;
039: private final TextFileTransformer fileTransformer;
040: private final EmbeddingConverter converter;
041: private final SystemLogger logger;
042: private final SourceMask mask;
043: private final boolean uptodatecheck;
044: private final OperationMode mode;
045: private int countTransformed;
046:
047: public FileTranslator(ClassTransformer classTransformer,
048: TextFileTransformer fileTransformer,
049: EmbeddingConverter converter, SystemLogger logger,
050: SourceMask mask, boolean uptodatecheck, OperationMode mode) {
051: this .classTransformer = classTransformer;
052: this .fileTransformer = fileTransformer;
053: this .converter = converter;
054: this .logger = logger;
055: this .mask = mask;
056: this .uptodatecheck = uptodatecheck;
057: this .mode = mode;
058: }
059:
060: public boolean transform(FileContainer source,
061: FileContainer destination) {
062: countTransformed = 0;
063: logger.log(new Message(Level.INFO, "Processing "
064: + source.getFileCount()
065: + " file(s)"
066: + (source == destination ? " in " + source : " from "
067: + source + " to " + destination) + "."));
068: if (uptodatecheck
069: && source.lastModified() < destination.lastModified()) {
070: logger.log(new Message(Level.INFO,
071: "Skipped up-to-date file(s)."));
072: return false;
073: }
074: for (FileEntry entry : source.getEntries()) {
075: transform(entry, source, destination);
076: }
077: logger.log(new Message(Level.INFO, "Transformed "
078: + countTransformed + " file(s)."));
079: if (converter != null && source == destination) {
080: converter.embed(source, classTransformer);
081: }
082: source.flush(logger);
083: return true;
084: }
085:
086: private void transform(FileEntry entry, FileContainer source,
087: FileContainer destination) {
088: String name = entry.getName();
089: String fixedName = mode.fixFileName(converter == null ? name
090: : converter.convertFileName(name), entry);
091: if (uptodatecheck
092: && destination.containsUpToDate(fixedName, entry
093: .lastModified())) {
094: logger.logForFile(Level.VERBOSE, "Up to date");
095: return;
096: }
097: if (mask.matches(name) || !name.equals(fixedName)) {
098: logger.setFile(source.getLocation(), name);
099: logger.logForFile(Level.VERBOSE, "Transformation");
100: byte[] sourceData = entry.getContent();
101: byte[] resultData = TransformerTools
102: .isClassFile(sourceData) ? classTransformer
103: .transform(sourceData, 0, sourceData.length)
104: : fileTransformer.transform(sourceData);
105: boolean transformed = sourceData != resultData
106: || !fixedName.equals(name);
107: if (transformed || source != destination) {
108: if (!fixedName.equals(name)) {
109: destination.removeEntry(name);
110: }
111: destination
112: .putEntry(fixedName, resultData, transformed);
113: }
114: if (transformed) {
115: countTransformed++;
116: }
117: logger.setFile(null, null);
118: } else if (source != destination) {
119: destination.putEntry(name, entry.getContent(), false);
120: }
121: }
122:
123: }
|