1 5 package com.tc.config; 6 7 import org.apache.xmlbeans.SchemaType; 8 import org.apache.xmlbeans.XmlException; 9 import org.apache.xmlbeans.XmlOptions; 10 11 import com.tc.config.schema.migrate.ConfigUpdate; 12 import com.tc.config.schema.migrate.V1toV2; 13 import com.tc.config.schema.migrate.V2toV3; 14 15 import java.io.ByteArrayInputStream ; 16 import java.io.File ; 17 import java.io.FileInputStream ; 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.net.URL ; 21 22 25 public final class Loader { 26 27 private final ConfigUpdate[] converters; 28 29 public Loader() { 30 converters = new ConfigUpdate[] { new V2toV3(), new V1toV2() }; 32 } 33 34 private com.terracottatech.config.TcConfigDocument convert(InputStream in, XmlOptions xmlOptions) throws IOException , 35 XmlException { 36 byte[] data = new byte[in.available()]; 37 in.read(data); 38 in.close(); 39 ByteArrayInputStream ain = new ByteArrayInputStream (data); 40 SchemaType type = com.terracottatech.config.TcConfigDocument.type; 41 if (xmlOptions == null) xmlOptions = new XmlOptions(); 42 xmlOptions.setDocumentType(type); 43 try { 44 return com.terracottatech.config.TcConfigDocument.Factory.parse(ain, xmlOptions); 45 } catch (XmlException e) { 46 ain.reset(); 47 return com.terracottatech.config.TcConfigDocument.Factory.parse(updateConfig(ain, 0, xmlOptions), xmlOptions); 48 } 49 } 50 51 private synchronized InputStream updateConfig(InputStream in, int index, XmlOptions xmlOptions) throws IOException , 52 XmlException { 53 byte[] data = new byte[in.available()]; 54 in.read(data); 55 in.close(); 56 ByteArrayInputStream ain = new ByteArrayInputStream (data); 57 try { 58 return converters[index].convert(ain, xmlOptions); 59 } catch (XmlException e) { 60 if (index == converters.length - 1) throw e; 61 ain.reset(); 62 return converters[index].convert(updateConfig(ain, index + 1, xmlOptions), xmlOptions); 63 } 64 } 65 66 public com.terracottatech.config.TcConfigDocument parse(File file) throws IOException , XmlException { 67 return convert(new FileInputStream (file), null); 68 } 69 70 public com.terracottatech.config.TcConfigDocument parse(File file, XmlOptions xmlOptions) throws IOException , 71 XmlException { 72 return convert(new FileInputStream (file), xmlOptions); 73 } 74 75 public com.terracottatech.config.TcConfigDocument parse(String xmlText) throws IOException , XmlException { 76 return convert(new ByteArrayInputStream (xmlText.getBytes()), null); 77 } 78 79 public com.terracottatech.config.TcConfigDocument parse(String xmlText, XmlOptions xmlOptions) throws IOException , 80 XmlException { 81 return convert(new ByteArrayInputStream (xmlText.getBytes()), xmlOptions); 82 } 83 84 public com.terracottatech.config.TcConfigDocument parse(InputStream stream) throws IOException , XmlException { 85 return convert(stream, null); 86 } 87 88 public com.terracottatech.config.TcConfigDocument parse(InputStream stream, XmlOptions xmlOptions) 89 throws IOException , XmlException { 90 return convert(stream, xmlOptions); 91 } 92 93 public com.terracottatech.config.TcConfigDocument parse(URL url) throws IOException , XmlException { 94 return convert(url.openStream(), null); 95 } 96 97 public com.terracottatech.config.TcConfigDocument parse(URL url, XmlOptions xmlOptions) throws IOException , 98 XmlException { 99 return convert(url.openStream(), xmlOptions); 100 } 101 102 public boolean testIsOld(File file) throws IOException , XmlException { 103 return !testIsCurrent(file); 104 } 105 106 public boolean testIsCurrent(File file) throws IOException , XmlException { 107 com.terracottatech.config.TcConfigDocument.Factory.parse(new FileInputStream (file)); 108 return true; 109 } 110 111 public void updateToCurrent(File file) throws IOException , XmlException { 112 XmlOptions options = null; 113 synchronized (converters) { 114 options = converters[0].createDefaultXmlOptions(); 115 } 116 com.terracottatech.config.TcConfigDocument doc = convert(new FileInputStream (file), options); 117 doc.save(file); 118 } 119 } 120 | Popular Tags |