6. public class CodeSource { List<CustomError> errorList = new ArrayList<CustomError>(); public CodeSource() { // fill the error list CustomError error = new CustomError(); error.setCode(101); error.setMessage( "Is there an error in this code?" ); errorList .add(error); // create the source Source source = new Source(); source.setProblem(101); // raise an error source.error( errorList ); } }
7. public class CustomError { private int code ; private String message ; public String toString() { return "Error #" + code + ": " + message ; } public int getCode() { return this . code ; } public void setCode( int code) { this . code = code; } public void setMessage(String message) { this . message = message; } }
8. public class Source { private int problem ; public void setProblem( int problem) { this . problem = problem; } public void error(List<CustomError> errorList) { for (CustomError e: errorList) { if (e.getCode() == this . problem ) { System. err .println(e); return ; } } System. err .println( "Error: error not found" ); } }
81. public SimpleSource(InputStream input) { try { Serializer serializer = new Persister(); Config config = (Config)serializer .read(Config. class , input); config.error(); serializer.write( config, new File("simple.xml")); } catch (Exception e) { e.printStackTrace(); } }
82. @Root (name= "config" ) public class Config { @ElementList (name= "handler" ) private List<CustomError> list ; @Element private Source source ; public void error() { this . source .error( list ); } }
83. public class CustomError { @Element private int code ; @Element private String message ; public String toString() { return "Error #" + code + ": " + message ; } public int getCode() { return this . code ; } public void setCode( int code) { this . code = code; } public void setMessage(String message) { this . message = message; } }
84. public class Source { @Element private int problem ; public void setProblem( int problem) { this . problem = problem; } public void error(List<CustomError> errorList) { for (CustomError e: errorList) { if (e.getCode() == this . problem ) { System. err .println(e); return ; } } System. err .println( "Error: error not found" ); } }