1 18 package org.enhydra.snapper.business.xml; 19 20 import java.io.File ; 22 import java.io.FileReader ; 23 import java.io.InputStream ; 24 import java.io.InputStreamReader ; 25 import java.io.IOException ; 26 import java.io.Reader ; 27 import org.xml.sax.EntityResolver ; 28 import org.xml.sax.ErrorHandler ; 29 import org.xml.sax.SAXException ; 30 import org.xml.sax.SAXParseException ; 31 32 public class SummaryUnmarshaller { 33 34 35 private static EntityResolver entityResolver; 36 37 38 private static ErrorHandler errorHandler; 39 40 47 public static void setEntityResolver(EntityResolver resolver) { 48 entityResolver = resolver; 49 } 50 51 58 public static void setErrorHandler(ErrorHandler handler) { 59 errorHandler = handler; 60 } 61 62 public static Summary unmarshal(File file) throws IOException { 63 return unmarshal(new FileReader (file)); 65 } 66 67 public static Summary unmarshal(File file, boolean validate) throws IOException { 68 return unmarshal(new FileReader (file), validate); 70 } 71 72 public static Summary unmarshal(InputStream inputStream) throws IOException { 73 return unmarshal(new InputStreamReader (inputStream)); 75 } 76 77 public static Summary unmarshal(InputStream inputStream, boolean validate) throws IOException { 78 return unmarshal(new InputStreamReader (inputStream), validate); 80 } 81 82 public static Summary unmarshal(Reader reader) throws IOException { 83 String property = System.getProperty("org.enhydra.zeus.validation", "false"); 85 boolean validationState = false; 86 if (property.equalsIgnoreCase("true")) { 87 validationState = true; 88 } 89 90 return unmarshal(reader, validationState); 92 } 93 94 public static Summary unmarshal(Reader reader, boolean validate) throws IOException { 95 if (entityResolver != null) { 97 SummaryImpl.setEntityResolver(entityResolver); 98 } 99 100 if (errorHandler != null) { 102 SummaryImpl.setErrorHandler(errorHandler); 103 } else { 104 if (validate) { 105 SummaryImpl.setErrorHandler(new SummaryDefaultErrorHandler()); 106 } 107 } 108 109 return SummaryImpl.unmarshal(reader, validate); 111 } 112 113 } 114 115 116 class SummaryDefaultErrorHandler implements ErrorHandler { 117 118 public void warning(SAXParseException e) throws SAXException { 119 System.err.println("Parsing Warning: " + e.getMessage()); 120 } 121 122 public void error(SAXParseException e) throws SAXException { 123 System.err.println("Parsing Error: " + e.getMessage()); 124 throw e; 125 } 126 127 public void fatalError(SAXParseException e) throws SAXException { 128 System.err.println("Fatal Parsing Error: " + e.getMessage()); 129 throw e; 130 } 131 132 } 133 | Popular Tags |