📄 errorcode.java
字号:
} /** * The link to find more information about this problem */ public String getUrl() { return "http://www.xmlblaster.org/xmlBlaster/doc/requirements/admin.errorcodes.listing.html#" + getErrorCode(); } /** * Return resource info object telling us where to find more information * on this errorCode */ public ResourceInfo[] getResourceInfos() { return this.resourceInfos; } /** * @return The top level category like 'internal' */ public static ErrorCode getCategory(ErrorCode errorCode) { if (errorCode == null || errorCode.errorCode == null) return ErrorCode.INTERNAL; int index = errorCode.errorCode.indexOf("."); if (index == -1) return errorCode; // is already a top level if (index == 0) return ErrorCode.INTERNAL; // ".blabla" shouldn't appear (no leading dots) String top = errorCode.errorCode.substring(0,index); return toErrorCode(top); } /** * @return The top level category like 'internal' */ public static ErrorCode getCategory(String errorCode) { if (errorCode == null) return ErrorCode.INTERNAL; int index = errorCode.indexOf("."); if (index == -1) return ErrorCode.toErrorCode(errorCode); // is already a top level if (index == 0) return ErrorCode.INTERNAL; // ".blabla" shouldn't appear (no leading dots) String top = errorCode.substring(0,index); return toErrorCode(top); } /** * Returns the ErrorCode object for the given String error code. * @param errorCode The String code to lookup * @return The enumeration object for this errorCode * @exception IllegalArgumentException if the given errorCode is invalid */ public static final ErrorCode toErrorCode(String errorCode) throws IllegalArgumentException { if (errorCode == null) { throw new IllegalArgumentException("ErrorCode: The given errorCode=" + errorCode + " is null"); } Object entry = errorCodeMap.get(errorCode); if (entry == null) throw new IllegalArgumentException("ErrorCode: The given errorCode=" + errorCode + " is unknown"); return (ErrorCode)entry; } public final boolean equals(ErrorCode other) { return this.errorCode.equals(other.getErrorCode()); } /** * Dump a plain list of all errorCodes. * @return The list with each errorCode in a new line */ public static String toPlainList() { StringBuffer sb = new StringBuffer(2560); String offset = "\n"; java.util.Date date = new java.util.Date(); String d = new java.sql.Timestamp(date.getTime()).toString(); sb.append("# XmlBlaster ErrorCode listing " + d); Iterator it = errorCodeMap.keySet().iterator(); while (it.hasNext()) { String code = (String)it.next(); ErrorCode errorCode = (ErrorCode)errorCodeMap.get(code); sb.append(offset).append(errorCode.getErrorCode()); } return sb.toString(); } /** * Generate a HTML table listing of all error codes. * @return The HTML markup */ public static String toHtmlTable() { StringBuffer sb = new StringBuffer(2560); String offset = "\n "; sb.append(offset).append("<table border='1'>"); Iterator it = errorCodeMap.keySet().iterator(); sb.append(offset).append("<tr><th>Error Code</th><th>Description</th><th>See</th></tr>"); while (it.hasNext()) { sb.append(offset).append("<tr>"); String code = (String)it.next(); ErrorCode errorCode = (ErrorCode)errorCodeMap.get(code); sb.append(offset).append(" <td><a name='").append(errorCode.getErrorCode()).append("'></a>"); sb.append(errorCode.getErrorCode()).append("</td>"); String desc = ReplaceVariable.replaceAll(errorCode.getDescription(), "&", "&"); desc = ReplaceVariable.replaceAll(errorCode.getDescription(), "<", "<"); sb.append(offset).append(" <td>").append(desc).append("</td>"); ResourceInfo[] resourceInfos = errorCode.getResourceInfos(); sb.append(offset).append(" <td>"); for (int i=0; i<resourceInfos.length; i++) { if (i>0) sb.append("<br />"); String resource = ReplaceVariable.replaceAll(resourceInfos[i].getResource(), "<", "<"); String url=null; if (ResourceInfo.REQ.equals(resourceInfos[i].getType())) url="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/"+resource+".html"; else if (ResourceInfo.URL.equals(resourceInfos[i].getType())) url= resource; else if (ResourceInfo.API.equals(resourceInfos[i].getType())) { String replace = ReplaceVariable.replaceAll(resource, ".", "/"); url="http://www.xmlBlaster.org/xmlBlaster/doc/api/"+replace+".html"; } else { System.out.println("Ignoring unknown resource type '" + resourceInfos[i].getType() + "'"); continue; } sb.append("<a href='").append(url).append("' target='others'>"); sb.append(resourceInfos[i].getLabel()).append("</a>"); } if (resourceInfos.length == 0) sb.append("-"); sb.append("</td>"); sb.append(offset).append("</tr>"); } sb.append(offset).append("</table>"); return sb.toString(); } public static String toRequirement() { String req= "<?xml version='1.0' encoding='ISO-8859-1' ?>\n"+ "<!DOCTYPE requirement SYSTEM 'requirement.dtd'>\n" + "<requirement id='admin.errorcodes.listing' type='NEW' prio='LOW' status='CLOSED'>\n" + " <topic>XmlBlaster error code reference</topic>\n" + " <description>\n" + toHtmlTable() + "\nGenerated by org.xmlBlaster.util.def.ErrorCode\n" + " </description>\n" + "</requirement>"; return req; } public static String toXmlAll(String extraOffset) { StringBuffer sb = new StringBuffer(2560); String offset = "\n "; if (extraOffset == null) extraOffset = ""; offset += extraOffset; sb.append(offset).append("<ErrorCodes>"); Iterator it = errorCodeMap.keySet().iterator(); while (it.hasNext()) { String code = (String)it.next(); ErrorCode errorCode = (ErrorCode)errorCodeMap.get(code); sb.append(errorCode.toXml(" ")); } sb.append(offset).append("</ErrorCodes>"); return sb.toString(); } public String toXml(String extraOffset) { StringBuffer sb = new StringBuffer(256); String offset = "\n "; if (extraOffset == null) extraOffset = ""; offset += extraOffset; sb.append(offset).append("<errorCode id='").append(getErrorCode()).append("'>"); sb.append(offset).append(" <description>").append(getLongDescription()).append("</description>"); for (int i=0; i<resourceInfos.length; i++) sb.append(resourceInfos[i].toXml(extraOffset+" ")); sb.append(offset).append("</errorCode>"); return sb.toString(); } /** * This code is a helper for serialization so that after * deserial the check * <pre>ErrorCode.INTERNAL_UNKNOWN == internalUnknownInstance</pre> * is still usable (the singleton is assured when deserializing) * <br /> * See inner class SerializedForm */ public Object writeReplace() throws java.io.ObjectStreamException { return new SerializedForm(this.getErrorCode()); } /** * A helper class for singleton serialization. */ private static class SerializedForm implements java.io.Serializable { private static final long serialVersionUID = 1L; String errorCode; SerializedForm(String errorCode) { this.errorCode = errorCode; } Object readResolve() throws java.io.ObjectStreamException { return ErrorCode.toErrorCode(errorCode); } } /** * Generate a requirement file for all error codes. * Used by build.xml, change with care! * <pre> * java org.xmlBlaster.util.def.ErrorCode <HtmlFileName> * java org.xmlBlaster.util.def.ErrorCode verifySerialization * java org.xmlBlaster.util.def.ErrorCode toPlainList * </pre> */ public static void main(String [] args) { String file = "doc/requirements/admin.errorcodes.listing.xml"; if (args.length > 0) { file = args[0]; } if ("verifySerialization".equals(file)) { verifySerialization(); } else if ("toPlainList".equals(file)) { System.out.println(toPlainList()); } else { String req = toRequirement(); try { FileLocator.writeFile(file, req); System.out.println("Created requirement file '" + file + "'"); } catch (Exception e) { System.out.println("Writing file '" + file + "' failed: " + e.toString()); } } } private static void verifySerialization() { String fileName = "ErrorCode.ser"; ErrorCode pOrig = ErrorCode.USER_PTP_UNKNOWNSESSION; { try { java.io.FileOutputStream f = new java.io.FileOutputStream(fileName); java.io.ObjectOutputStream objStream = new java.io.ObjectOutputStream(f); objStream.writeObject(pOrig); objStream.flush(); System.out.println("SUCCESS written " + pOrig.toString()); } catch (Exception e) { System.err.println("ERROR: " + e.toString()); } } ErrorCode pNew = null; { try { java.io.FileInputStream f = new java.io.FileInputStream(fileName); java.io.ObjectInputStream objStream = new java.io.ObjectInputStream(f); pNew = (ErrorCode)objStream.readObject(); System.out.println("SUCCESS loaded " + pNew.toString()); } catch (Exception e) { System.err.println("ERROR: " + e.toString()); } } if (pNew.toString().equals(pOrig.toString())) { System.out.println("SUCCESS, string form is equals " + pNew.toString()); } else { System.out.println("ERROR, string form is different " + pNew.toString()); } int hashOrig = pOrig.hashCode(); int hashNew = pNew.hashCode(); if (pNew == pOrig) { System.out.println("SUCCESS, hash is same, the objects are identical"); } else { System.out.println("ERROR, hashCode is different hashOrig=" + hashOrig + " hashNew=" + hashNew); } }} /** * class holding reference data about other documentation locations */ final class ResourceInfo { public final static String REQ = "REQ"; public final static String API = "API"; public final static String URL = "URL"; private final String type; // "API", "REQ", "URL" private final String label; private final String resource; /** * @param type One of "API", "REQ", "URL" * @param label The visible name for the link * @param resource The classname (for API), the requirement name (for REQ) or the href url (for URL) */ public ResourceInfo(String type, String label, String resource) { if (!REQ.equalsIgnoreCase(type) && !API.equalsIgnoreCase(type) && !URL.equalsIgnoreCase(type)) throw new IllegalArgumentException("Construction of ResourceInfo with illegal type=" + type); if (label == null || label.length() < 1) throw new IllegalArgumentException("Construction of ResourceInfo with empty lable"); this.type = type.toUpperCase(); this.label = label; this.resource = (resource==null) ? "" : resource; } public String getType() { return this.type; } public String getLabel() { return this.label; } public String getResource() { return this.resource; } public final String toXml(String extraOffset) { StringBuffer sb = new StringBuffer(256); String offset = "\n "; if (extraOffset == null) extraOffset = ""; offset += extraOffset; sb.append(offset).append("<ResourceInfo type='").append(getType()).append("'"); sb.append(" label='").append(getLabel()).append("'>"); sb.append(offset).append(" ").append(getResource()); sb.append(offset).append("</ResourceInfo>"); return sb.toString(); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -