📄 bindingelement.java
字号:
this); } } else { vctx.addError("Attribute '" + name + "' not allowed on included binding", this); } } } } } } /** * Prevalidate all attributes of element in isolation. * * @param vctx validation context */ public void prevalidate(ValidationContext vctx) { // set the direction flags int index = -1; if (m_direction != null) { index = s_directionEnum.getValue(m_direction); if (index < 0) { vctx.addError("Value \"" + m_direction + "\" is not a valid choice for direction"); } } else { index = BOTH_BINDING; } m_isInput = index == IN_BINDING || index == BOTH_BINDING; m_isOutput = index == OUT_BINDING || index == BOTH_BINDING; super.prevalidate(vctx); } private static FormatElement buildFormat(String name, String type, boolean use, String sname, String dname, String dflt) { FormatElement format = new FormatElement(); format.setLabel(name); format.setTypeName(type); format.setDefaultFormat(use); format.setSerializerName(sname); format.setDeserializerName(dname); format.setDefaultText(dflt); return format; } private void defineBaseFormat(FormatElement format, DefinitionContext dctx, ValidationContext vctx) { format.prevalidate(vctx); format.validate(vctx); dctx.addFormat(format, vctx); } /** * Run the actual validation of a binding model. * * @param vctx context for controlling validation */ public void runValidation(ValidationContext vctx) { // initially enable both directions for format setup m_isInput = true; m_isOutput = true; // create outer definition context DefinitionContext dctx = new DefinitionContext(null); vctx.setGlobalDefinitions(dctx); defineBaseFormat(buildFormat("byte.default", "byte", true, "org.jibx.runtime.Utility.serializeByte", "org.jibx.runtime.Utility.parseByte", "0"), dctx, vctx); defineBaseFormat(buildFormat("char.default", "char", true, "org.jibx.runtime.Utility.serializeChar", "org.jibx.runtime.Utility.parseChar", "0"), dctx, vctx); defineBaseFormat(buildFormat("double.default", "double", true, "org.jibx.runtime.Utility.serializeDouble", "org.jibx.runtime.Utility.parseDouble", "0.0"), dctx, vctx); defineBaseFormat(buildFormat("float.default", "float", true, "org.jibx.runtime.Utility.serializeFloat", "org.jibx.runtime.Utility.parseFloat", "0.0"), dctx, vctx); defineBaseFormat(buildFormat("int.default", "int", true, "org.jibx.runtime.Utility.serializeInt", "org.jibx.runtime.Utility.parseInt", "0"), dctx, vctx); defineBaseFormat(buildFormat("long.default", "long", true, "org.jibx.runtime.Utility.serializeLong", "org.jibx.runtime.Utility.parseLong", "0"), dctx, vctx); defineBaseFormat(buildFormat("short.default", "short", true, "org.jibx.runtime.Utility.serializeShort", "org.jibx.runtime.Utility.parseShort", "0"), dctx, vctx); defineBaseFormat(buildFormat("boolean.default", "boolean", true, "org.jibx.runtime.Utility.serializeBoolean", "org.jibx.runtime.Utility.parseBoolean", "false"), dctx, vctx); defineBaseFormat(buildFormat("Date.default", "java.util.Date", true, "org.jibx.runtime.Utility.serializeDateTime", "org.jibx.runtime.Utility.deserializeDateTime", null), dctx, vctx);//#!j2me{ defineBaseFormat(buildFormat("SqlDate.default", "java.sql.Date", true, "org.jibx.runtime.Utility.serializeSqlDate", "org.jibx.runtime.Utility.deserializeSqlDate", null), dctx, vctx); defineBaseFormat(buildFormat("SqlTime.default", "java.sql.Time", true, "org.jibx.runtime.Utility.serializeSqlTime", "org.jibx.runtime.Utility.deserializeSqlTime", null), dctx, vctx); defineBaseFormat(buildFormat("SqlTimestamp.default", "java.sql.Timestamp", true, "org.jibx.runtime.Utility.serializeTimestamp", "org.jibx.runtime.Utility.deserializeTimestamp", null), dctx, vctx);//#j2me} defineBaseFormat(buildFormat("byte-array.default", "byte[]", true, "org.jibx.runtime.Utility.serializeBase64", "org.jibx.runtime.Utility.deserializeBase64", null), dctx, vctx); defineBaseFormat(buildFormat("String.default", "java.lang.String", true, null, null, null), dctx, vctx); defineBaseFormat(buildFormat("Object.default", "java.lang.Object", true, null, null, null), dctx, vctx); FormatElement format = buildFormat("char.string", "char", false, "org.jibx.runtime.Utility.serializeCharString", "org.jibx.runtime.Utility.deserializeCharString", "0"); format.setDefaultFormat(false); format.prevalidate(vctx); format.validate(vctx); dctx.addFormat(format, vctx); NamespaceElement ns = new NamespaceElement(); ns.setDefaultName("all"); ns.prevalidate(vctx); dctx.addNamespace(ns); // TODO: check for errors in basic configuration // create a definition context for the binding setDefinitions(new DefinitionContext(dctx)); // run the actual validation vctx.prevalidate(this); RegistrationVisitor rvisitor = new RegistrationVisitor(vctx); rvisitor.visitTree(this); vctx.validate(this); } /** * Read a binding definition (possibly as an include) to construct binding * model. * * @param is input stream for reading binding * @param fname name of input file (<code>null</code> if unknown) * @param contain containing binding (<code>null</code> if none) * @param vctx validation context used during unmarshalling * @return root of binding definition model * @throws JiBXException on error in reading binding */ public static BindingElement readBinding(InputStream is, String fname, BindingElement contain, ValidationContext vctx) throws JiBXException { // look up the binding factory IBindingFactory bfact = BindingDirectory.getFactory(BindingElement.class); // unmarshal document to construct objects IUnmarshallingContext uctx = bfact.createUnmarshallingContext(); uctx.setDocument(is, fname, null); uctx.pushObject(new UnmarshalWrapper(vctx, contain)); BindingElement binding = (BindingElement)uctx.unmarshalElement(); uctx.popObject(); return binding; } /** * Read a binding definition to construct binding model. * * @param is input stream for reading binding * @param fname name of input file (<code>null</code> if unknown) * @param vctx validation context used during unmarshalling * @return root of binding definition model * @throws JiBXException on error in reading binding */ public static BindingElement readBinding(InputStream is, String fname, ValidationContext vctx) throws JiBXException { return readBinding(is, fname, null, vctx); } /** * Validate a binding definition. * * @param name binding definition name * @param is input stream for reading binding * @param vctx validation context to record problems * @return root of binding definition model, or <code>null</code> if error * in unmarshalling * @throws JiBXException on error in binding XML structure */ public static BindingElement validateBinding(String name, URL path, InputStream is, ValidationContext vctx) throws JiBXException { // construct object model for binding BindingElement binding = readBinding(is, name, vctx); binding.setBaseUrl(path); vctx.setBindingRoot(binding); // validate the binding definition binding.runValidation(vctx); // list validation errors ArrayList probs = vctx.getProblems(); if (probs.size() > 0) { for (int i = 0; i < probs.size(); i++) { ValidationProblem prob = (ValidationProblem)probs.get(i); System.out.print(prob.getSeverity() >= ValidationProblem.ERROR_LEVEL ? "Error: " : "Warning: "); System.out.println(prob.getDescription()); } } return binding; } /** * Create a default validation context. * * @return new validation context */ public static ValidationContext newValidationContext() { IClassLocator locate = new IClassLocator() { public IClass getClassInfo(String name) { try { return new ClassWrapper(this, ClassCache.getClassFile(name)); } catch (JiBXException e) { return null; } } }; return new ValidationContext(locate); } /* // test runner // This code only used in testing, to roundtrip binding definitions public static void test(String ipath, String opath, ValidationContext vctx) throws Exception { // validate the binding definition FileInputStream is = new FileInputStream(ipath); URL url = new URL("file://" + ipath); BindingElement binding = validateBinding(ipath, url, is, vctx); // marshal back out for comparison purposes IBindingFactory bfact = BindingDirectory.getFactory(BindingElement.class); IMarshallingContext mctx = bfact.createMarshallingContext(); mctx.setIndent(2); mctx.marshalDocument(binding, null, null, new FileOutputStream(opath)); System.out.println("Wrote output binding " + opath); // compare input document with output document DocumentComparator comp = new DocumentComparator(System.err); boolean match = comp.compare(new FileReader(ipath), new FileReader(opath)); if (!match) { System.err.println("Mismatch from input " + ipath + " to output " + opath); } } // test runner public static void main(String[] args) throws Exception { // configure class loading String[] paths = new String[] { "." }; ClassCache.setPaths(paths); ClassFile.setPaths(paths); ValidationContext vctx = newValidationContext(); // process all bindings listed on command line for (int i = 0; i < args.length; i++) { try { String ipath = args[i]; int split = ipath.lastIndexOf(File.separatorChar); String opath = "x" + ipath.substring(split+1); test(ipath, opath, vctx); } catch (Exception e) { System.err.println("Error handling binding " + args[i]); e.printStackTrace(); } } } */ /** * Inner class as wrapper for binding element on unmarshalling. This * provides a handle for passing the validation context, allowing elements * to check for problems during unmarshalling. */ public static class UnmarshalWrapper { private final ValidationContext m_validationContext; private final BindingElement m_containingBinding; protected UnmarshalWrapper(ValidationContext vctx, BindingElement contain) { m_validationContext = vctx; m_containingBinding = contain; } public ValidationContext getValidation() { return m_validationContext; } public BindingElement getContainingBinding() { return m_containingBinding; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -