1 23 package org.ofbiz.webapp.barcode; 24 25 import java.io.BufferedReader ; 26 import java.io.IOException ; 27 import java.io.InputStreamReader ; 28 import java.net.URL ; 29 import java.util.HashMap ; 30 import java.util.Map ; 31 32 import org.ofbiz.base.util.Debug; 33 import org.ofbiz.base.util.UtilURL; 34 35 48 public class BarcodeFactory { 49 50 public static final String module = BarcodeFactory.class.getName(); 51 52 private static Map barcodeGeneratorMap = new HashMap (); 53 54 60 public static BarcodeGenerator getBarcodeGenerator(String barcodeType) { 61 synchronized (barcodeGeneratorMap) { 62 if (barcodeGeneratorMap.containsKey(barcodeType)) { 63 return (BarcodeGenerator) barcodeGeneratorMap.get(barcodeType); 64 } 65 else { 66 String specificationFile = "barcode/" + barcodeType + ".xml"; 67 68 URL specificationUrl = UtilURL.fromResource(specificationFile); 69 if (specificationUrl == null) { 70 Debug.logError("Problem getting the specification URL: " + specificationFile + " not found", module); 71 return null; 72 } 73 74 StringBuffer format = new StringBuffer (); 75 BufferedReader specificationReader = null; 76 try { 77 specificationReader = new BufferedReader (new InputStreamReader (specificationUrl.openStream())); 78 String line = null; 79 while ((line = specificationReader.readLine()) != null) { 80 format.append(line); 81 } 82 } 83 catch (java.io.IOException e) { 84 Debug.logError(e, "Error reading the specification URL: " + specificationFile, module); 85 return null; 86 } 87 finally { 88 if (specificationReader != null) { 89 try { 90 specificationReader.close(); 91 } 92 catch (IOException e) { 93 Debug.logError(e, "Error closing the specification URL: " + specificationFile, module); 94 return null; 95 } 96 } 97 } 98 99 BarcodeGenerator generator = new BarcodeGenerator(format.toString()); 100 barcodeGeneratorMap.put(barcodeType, generator); 101 return generator; 102 } 103 } 104 } 105 106 } 107 | Popular Tags |