1 package it.stefanochizzolini.clown.samples; 2 3 import it.stefanochizzolini.clown.bytes.OutputStream; 4 import it.stefanochizzolini.clown.files.File; 5 import it.stefanochizzolini.clown.files.SerializationModeEnum; 6 import it.stefanochizzolini.reflex.Package; 7 8 import java.io.BufferedOutputStream ; 9 import java.io.FileInputStream ; 10 import java.io.FileOutputStream ; 11 import java.util.ArrayList ; 12 import java.util.Arrays ; 13 import java.util.List ; 14 import java.util.Properties ; 15 import java.util.Scanner ; 16 17 public class PDFClownSampleLoader 18 { 19 private static final String ClassName = PDFClownSampleLoader.class.getName(); 22 23 private static final String PropertiesFilePath = "PDFClownSamples.properties"; 24 25 private static final String Properties_InputPath = ClassName + ".inputPath"; 26 private static final String Properties_OutputPath = ClassName + ".outputPath"; 27 29 public static void main( 32 String [] args 33 ) 34 { 35 System.out.println("\nPDFClownSampleLoader running...\n"); 36 37 try 38 { 39 java.lang.Package pdfClownPackage = Class.forName("it.stefanochizzolini.clown.Application").getPackage(); 40 System.out.println(pdfClownPackage.getSpecificationTitle() + " version " + pdfClownPackage.getImplementationVersion()); 41 } 42 catch(Exception e) 43 {throw new RuntimeException ("Unable to detect PDFClown library version.",e);} 44 45 Properties properties = new Properties (); 46 try 47 { 48 properties.load( 49 new FileInputStream (PropertiesFilePath) 50 ); 51 } 52 catch(Exception e) 53 {throw new RuntimeException ("An exception occurred while loading the properties file (\"" + PropertiesFilePath + "\").",e);} 54 55 try 56 { 57 PDFClownSampleLoader loader = new PDFClownSampleLoader( 58 properties.getProperty(Properties_InputPath), 59 properties.getProperty(Properties_OutputPath) 60 ); 61 loader.run(); 62 } 63 catch(Exception e) 64 {throw new RuntimeException (e);} 65 66 System.out.println("\nPDFClownSampleLoader finished.\n"); 67 } 68 72 private String inputPath; 75 private String outputPath; 76 78 public PDFClownSampleLoader( 80 String inputPath, 81 String outputPath 82 ) 83 { 84 this.inputPath = new java.io.File (inputPath).getAbsolutePath(); 85 this.outputPath = new java.io.File (outputPath).getAbsolutePath(); 86 } 87 89 public String getFileChoice( 92 String fileExtension, 93 String listDescription, 94 String inputDescription 95 ) 96 { 97 Scanner in = new Scanner (System.in); 98 99 System.out.println("\n" + listDescription + ":"); 100 SampleResources resources = new SampleResources(new java.io.File (inputPath + java.io.File.separator + "pdf" + java.io.File.separator)); 101 String [] filePaths = resources.filter(fileExtension); 103 resources.printList(filePaths); 105 System.out.print(inputDescription + ": "); 107 try 108 {return inputPath + java.io.File.separator + "pdf" + java.io.File.separator + filePaths[Integer.parseInt(in.nextLine())]; } 109 catch(Exception e) 110 {return inputPath + java.io.File.separator + "pdf" + java.io.File.separator + filePaths[0]; } 111 } 112 113 public String getInputPath( 114 ) 115 {return inputPath;} 116 117 public String getOutputPath( 118 ) 119 {return outputPath;} 120 121 public String getPdfFileChoice( 122 String inputDescription 123 ) 124 { 125 return getFileChoice( 126 "pdf", 127 "Available PDF files", 128 inputDescription 129 ); 130 } 131 132 public void run( 133 ) 134 { 135 Scanner in = new Scanner (System.in); 136 String samplePackageName = PDFClownSampleLoader.class.getPackage().getName(); 137 while(true) 138 { 139 143 List <Class > samplePackageClasses = Package.getClasses( 144 samplePackageName, new String [] 146 { 147 it.stefanochizzolini.reflex.Class.getLocation(ClassName) 148 } ); 150 Class sampleInterface = ISample.class; 151 152 System.out.println("\nAvailable samples:"); 153 List <java.lang.Class > sampleClasses = new ArrayList <java.lang.Class >(); 155 for(Class samplePackageClass : samplePackageClasses) 157 { 158 if( 159 Arrays.asList( 160 samplePackageClass.getInterfaces() 161 ).contains(sampleInterface) 162 ) 163 { 164 sampleClasses.add(samplePackageClass); 165 System.out.println("[" + sampleClasses.indexOf(samplePackageClass) + "] " + samplePackageClass.getSimpleName()); 166 } 167 } 168 System.out.println("[Q] (Quit)"); 169 170 Class sampleClass = null; 172 System.out.print("Please select a sample: "); 173 try 174 { 175 String choice = in.nextLine(); 176 if(choice.toUpperCase().equals("Q")) break; 178 179 sampleClass = sampleClasses.get(Integer.parseInt(choice)); } 181 catch(Exception e) 182 { 183 sampleClass = sampleClasses.get(0); } 185 186 System.out.println("\nInstantiating " + sampleClass.getName() + " sample..."); 187 188 ISample sample; 190 try 191 {sample = (ISample)sampleClass.newInstance();} 192 catch(Exception e) 193 {throw new RuntimeException (sampleClass.getName() + " sample class has failed to instantiate.",e);} 194 195 System.out.println("\nRunning " + sampleClass.getName() + " sample..."); 196 197 try 198 { 199 sample.run(this); 201 } 202 catch(Exception e) 203 {System.out.println("An exception happened while running the sample:");e.printStackTrace();continue;} 204 } 205 } 206 207 public void serialize( 208 File file, 209 String outputFileName 210 ) 211 { 212 serialize( 213 file, 214 outputFileName, 215 true 216 ); 217 } 218 219 public void serialize( 220 File file, 221 String outputFileName, 222 boolean chooseMode 223 ) 224 { 225 Scanner in = new Scanner (System.in); 226 227 SerializationModeEnum serializationMode = SerializationModeEnum.Incremental; 228 if(chooseMode) 229 { 230 System.out.println("[0] Standard serialization"); 231 System.out.println("[1] Incremental update"); 232 System.out.print("Please select a serialization mode: "); 234 try 235 {serializationMode = SerializationModeEnum.values()[Integer.parseInt(in.nextLine())];} 236 catch(Exception e) 237 {} 238 } 239 240 java.io.File outputFile = new java.io.File (outputPath + java.io.File.separator + outputFileName + "." + serializationMode + ".pdf"); 241 OutputStream outputStream; 242 BufferedOutputStream baseOutputStream; 243 try 244 { 245 outputFile.createNewFile(); 246 baseOutputStream = new BufferedOutputStream ( 247 new FileOutputStream (outputFile) 248 ); 249 outputStream = new it.stefanochizzolini.clown.bytes.OutputStream(baseOutputStream); 250 } 251 catch(Exception e) 252 {throw new RuntimeException (outputFile.getPath() + " file couldn't be created.",e);} 253 254 try 255 { 256 file.writeTo( 257 outputStream, 258 serializationMode 259 ); 260 261 baseOutputStream.flush(); 262 baseOutputStream.close(); 263 } 264 catch(Exception e) 265 {throw new RuntimeException (outputFile.getPath() + " file writing has failed.",e);} 266 267 System.out.println("Output: " + outputFile.getPath()); 268 } 269 } | Popular Tags |