View Javadoc

1   package org.apache.torque.mojo.morph;
2   
3   import java.io.IOException;
4   import java.io.InputStream;
5   import java.io.OutputStream;
6   
7   import org.apache.commons.io.IOUtils;
8   import org.apache.commons.logging.Log;
9   import org.apache.commons.logging.LogFactory;
10  
11  public abstract class Morpher {
12  	private static final Log log = LogFactory.getLog(Morpher.class);
13  
14  	MorphRequest morphRequest;
15  	String artifactId;
16  
17  	public Morpher() {
18  		this(null, null);
19  	}
20  
21  	public Morpher(MorphRequest morphRequest, String artifactId) {
22  		super();
23  		this.morphRequest = morphRequest;
24  		this.artifactId = artifactId;
25  	}
26  
27  	protected abstract boolean isMorphNeeded(String contents);
28  
29  	protected abstract String getMorphedContents(String contents);
30  
31  	public void executeMorph() throws IOException {
32  		// Read the "old" data into a string
33  		InputStream in = morphRequest.getInputStream();
34  		String contents = IOUtils.toString(in, morphRequest.getEncoding());
35  		IOUtils.closeQuietly(in);
36  
37  		// May not need to morph
38  		if (isMorphNeeded(contents)) {
39  			contents = getMorphedContents(contents);
40  		} else {
41  			log.debug("Skipping morph on " + morphRequest.getName());
42  		}
43  
44  		// Write the morphed data to the output stream
45  		OutputStream out = morphRequest.getOutputStream();
46  		IOUtils.write(contents, out, morphRequest.getEncoding());
47  		IOUtils.closeQuietly(out);
48  	}
49  
50  	public MorphRequest getMorphRequest() {
51  		return morphRequest;
52  	}
53  
54  	public void setMorphRequest(MorphRequest morphRequest) {
55  		this.morphRequest = morphRequest;
56  	}
57  
58  	public String getArtifactId() {
59  		return artifactId;
60  	}
61  
62  	public void setArtifactId(String schema) {
63  		this.artifactId = schema;
64  	}
65  
66  }