001: package it.stefanochizzolini.clown.samples;
002:
003: import it.stefanochizzolini.clown.bytes.OutputStream;
004: import it.stefanochizzolini.clown.documents.Document;
005: import it.stefanochizzolini.clown.documents.interaction.ViewerPreferences;
006: import it.stefanochizzolini.clown.documents.interchange.metadata.Information;
007: import it.stefanochizzolini.clown.files.File;
008: import it.stefanochizzolini.clown.files.SerializationModeEnum;
009: import it.stefanochizzolini.reflex.Package;
010:
011: import java.io.BufferedOutputStream;
012: import java.io.FileInputStream;
013: import java.io.FileOutputStream;
014: import java.util.ArrayList;
015: import java.util.Arrays;
016: import java.util.Date;
017: import java.util.List;
018: import java.util.Properties;
019: import java.util.Scanner;
020:
021: public class PDFClownSampleLoader {
022: // <static>
023: // <fields>
024: private static final String ClassName = PDFClownSampleLoader.class
025: .getName();
026:
027: private static final String PropertiesFilePath = "PDFClownSamples.properties";
028:
029: private static final String Properties_InputPath = ClassName
030: + ".inputPath";
031: private static final String Properties_OutputPath = ClassName
032: + ".outputPath";
033:
034: // </fields>
035:
036: // <interface>
037: // <public>
038: public static void main(String[] args) {
039: System.out.println("\nPDFClownSampleLoader running...\n");
040:
041: try {
042: java.lang.Package pdfClownPackage = Class.forName(
043: "it.stefanochizzolini.clown.Application")
044: .getPackage();
045: System.out.println(pdfClownPackage.getSpecificationTitle()
046: + " version "
047: + pdfClownPackage.getImplementationVersion());
048: } catch (Exception e) {
049: throw new RuntimeException(
050: "Unable to detect PDFClown library version.", e);
051: }
052:
053: Properties properties = new Properties();
054: try {
055: properties.load(new FileInputStream(PropertiesFilePath));
056: } catch (Exception e) {
057: throw new RuntimeException(
058: "An exception occurred while loading the properties file (\""
059: + PropertiesFilePath + "\").", e);
060: }
061:
062: try {
063: PDFClownSampleLoader loader = new PDFClownSampleLoader(
064: properties.getProperty(Properties_InputPath),
065: properties.getProperty(Properties_OutputPath));
066: loader.run();
067: } catch (Exception e) {
068: throw new RuntimeException(e);
069: }
070:
071: System.out.println("\nPDFClownSampleLoader finished.\n");
072: }
073:
074: // </public>
075: // </interface>
076: // </static>
077:
078: // <dynamic>
079: // <fields>
080: private String inputPath;
081: private String outputPath;
082:
083: // </fields>
084:
085: // <constructors>
086: public PDFClownSampleLoader(String inputPath, String outputPath) {
087: this .inputPath = new java.io.File(inputPath).getAbsolutePath();
088: this .outputPath = new java.io.File(outputPath)
089: .getAbsolutePath();
090: }
091:
092: // </constructors>
093:
094: // <interface>
095: // <public>
096: public String getFileChoice(String fileExtension,
097: String listDescription, String inputDescription) {
098: Scanner in = new Scanner(System.in);
099:
100: System.out.println("\n" + listDescription + ":");
101: SampleResources resources = new SampleResources(
102: new java.io.File(inputPath + java.io.File.separator
103: + "pdf" + java.io.File.separator));
104: // Get the list of available PDF files!
105: String[] filePaths = resources.filter(fileExtension);
106: // Display files!
107: resources.printList(filePaths);
108: // Get the user's choice!
109: System.out.print(inputDescription + ": ");
110: try {
111: return inputPath + java.io.File.separator + "pdf"
112: + java.io.File.separator
113: + filePaths[Integer.parseInt(in.nextLine())]; /* Custom choice. */
114: } catch (Exception e) {
115: return inputPath + java.io.File.separator + "pdf"
116: + java.io.File.separator + filePaths[0]; /* Default choice. */
117: }
118: }
119:
120: public String getInputPath() {
121: return inputPath;
122: }
123:
124: public String getOutputPath() {
125: return outputPath;
126: }
127:
128: public String getPdfFileChoice(String inputDescription) {
129: return getFileChoice("pdf", "Available PDF files",
130: inputDescription);
131: }
132:
133: public void run() {
134: Scanner in = new Scanner(System.in);
135: String samplePackageName = PDFClownSampleLoader.class
136: .getPackage().getName();
137: while (true) {
138: // Get the classes belonging to the current package!
139: /*
140: NOTE: We restrict our retrieval to the current deployment unit only.
141: */
142: List<Class> samplePackageClasses = Package.getClasses(
143: samplePackageName, // Package: current package.
144: new String[] { it.stefanochizzolini.reflex.Class
145: .getLocation(ClassName) } // Locations: current deployment unit only.
146: );
147: Class sampleInterface = ISample.class;
148:
149: System.out.println("\nAvailable samples:");
150: // Instantiate the list of available samples!
151: List<java.lang.Class> sampleClasses = new ArrayList<java.lang.Class>();
152: // Picking available samples...
153: for (Class samplePackageClass : samplePackageClasses) {
154: if (Arrays.asList(samplePackageClass.getInterfaces())
155: .contains(sampleInterface)) {
156: sampleClasses.add(samplePackageClass);
157: System.out
158: .println("["
159: + sampleClasses
160: .indexOf(samplePackageClass)
161: + "] "
162: + samplePackageClass
163: .getSimpleName());
164: }
165: }
166: System.out.println("[Q] (Quit)");
167:
168: // Getting the user's choice...
169: Class sampleClass = null;
170: System.out.print("Please select a sample: ");
171: try {
172: String choice = in.nextLine();
173: if (choice.toUpperCase().equals("Q")) // Quit.
174: break;
175:
176: sampleClass = sampleClasses.get(Integer
177: .parseInt(choice)); // Custom choice.
178: } catch (Exception e) {
179: sampleClass = sampleClasses.get(0); // Default choice.
180: }
181:
182: System.out.println("\nInstantiating "
183: + sampleClass.getName() + " sample...");
184:
185: // Instantiate the sample!
186: ISample sample;
187: try {
188: sample = (ISample) sampleClass.newInstance();
189: } catch (Exception e) {
190: throw new RuntimeException(sampleClass.getName()
191: + " sample class has failed to instantiate.", e);
192: }
193:
194: System.out.println("\nRunning " + sampleClass.getName()
195: + " sample...");
196:
197: try {
198: // Run the sample!
199: sample.run(this );
200: } catch (Exception e) {
201: System.out
202: .println("An exception happened while running the sample:");
203: e.printStackTrace();
204: continue;
205: }
206: }
207: }
208:
209: public void serialize(File file, String outputFileName) {
210: serialize(file, outputFileName, true);
211: }
212:
213: public void serialize(File file, String outputFileName,
214: boolean chooseMode) {
215: Scanner in = new Scanner(System.in);
216:
217: SerializationModeEnum serializationMode = SerializationModeEnum.Incremental;
218: if (chooseMode) {
219: System.out.println("[0] Standard serialization");
220: System.out.println("[1] Incremental update");
221: // Get the user's choice.
222: System.out.print("Please select a serialization mode: ");
223: try {
224: serializationMode = SerializationModeEnum.values()[Integer
225: .parseInt(in.nextLine())];
226: } catch (Exception e) {/* Default. */
227: }
228: }
229:
230: java.io.File outputFile = new java.io.File(outputPath
231: + java.io.File.separator + outputFileName + "."
232: + serializationMode + ".pdf");
233:
234: // Save the file!
235: file.writeTo(outputFile, serializationMode);
236:
237: // Alternatively, defining an appropriate target stream:
238: /*
239: OutputStream outputStream;
240: BufferedOutputStream baseOutputStream;
241: try
242: {
243: outputFile.createNewFile();
244: baseOutputStream = new BufferedOutputStream(
245: new FileOutputStream(outputFile)
246: );
247: outputStream = new it.stefanochizzolini.clown.bytes.OutputStream(baseOutputStream);
248: }
249: catch(Exception e)
250: {throw new RuntimeException(outputFile.getPath() + " file couldn't be created.",e);}
251:
252: try
253: {
254: file.writeTo(
255: outputStream,
256: serializationMode
257: );
258:
259: baseOutputStream.flush();
260: baseOutputStream.close();
261: }
262: catch(Exception e)
263: {throw new RuntimeException(outputFile.getPath() + " file writing has failed.",e);}
264: */
265: System.out.println("Output: " + outputFile.getPath());
266: }
267:
268: public void buildAccessories(Document document, Class creator,
269: String title, String subject) {
270: // Viewer preferences.
271: ViewerPreferences view = new ViewerPreferences(document); // Instanciate viewer preferences inside the document context.
272: document.setViewerPreferences(view); // Assign the viewer preferences object to the viewer preferences function.
273: view.setDisplayDocTitle(true);
274:
275: // Document metadata.
276: Information info = new Information(document);
277: document.setInformation(info);
278: info.setAuthor("Stefano Chizzolini");
279: info.setCreationDate(new Date());
280: info.setCreator(creator.getName());
281: info.setTitle("PDF Clown - " + title + " sample");
282: info.setSubject("Sample about " + subject + " in PDF Clown");
283: }
284: // </public>
285: // </interface>
286: // </dynamic>
287: }
|