📄 utility.java
字号:
int split = path.lastIndexOf(File.separatorChar); return path.substring(split+1); } /** * Validate binding definition. If issues are found in the binding the * issues are printed directly to the console. * * @param name identifier for binding definition * @param url URL for binding definition (<code>null</code> if not * available) * @param is input stream for reading binding definition * @return root element of binding model if binding is valid, * <code>null</code> if one or more errors in binding */ public static BindingElement validateBinding(String name, URL url, InputStream is) { try { ValidationContext vctx = BindingElement.newValidationContext(); BindingElement root = BindingElement.validateBinding(name, url, is, vctx); if (vctx.getErrorCount() == 0 && vctx.getFatalCount() == 0) { return root; } } catch (JiBXException ex) { System.err.println("Unable to process binding " + name); ex.printStackTrace(); } return null; } /** * Load validated binding definition. This first reads the input stream into * a memory buffer, then parses the data once for validation and a second * time for the actual binding definition construction. If any errors are * found in the binding definition validation the construction is skipped * and an exception is thrown. * * @param fname binding definition full name * @param sname short form of name to use as the default name of the binding * @param istrm input stream for binding definition document * @param url URL for binding definition (<code>null</code> if not * available) * @param test validate binding flag * @return constructed binding definition * @exception FileNotFoundException if path cannot be accessed * @exception JiBXException if error in processing the binding definition */ public static BindingDefinition loadBinding(String fname, String sname, InputStream istrm, URL url, boolean test) throws JiBXException, IOException { // read stream into memory buffer byte[] data = getStreamData(istrm); // validate using binding object model boolean valid = true; ClassFile cf = null; String tpack = null; if (test) { BindingElement root = validateBinding(fname, url, new ByteArrayInputStream(data)); if (root == null) { valid = false; } else { // find package of first mapping to use for added classes cf = findMappedClass(root); tpack = root.getTargetPackage(); if (tpack == null && cf != null) { tpack = cf.getPackage(); } } } if (valid) { try { // construct the binding definition code generator UnmarshallingContext uctx = new UnmarshallingContext(); uctx.setDocument(new ByteArrayInputStream(data), fname, null); if (cf != null) { // set target root and package for created classes BoundClass.setModify(cf.getRoot(), tpack); } BindingDefinition bdef = BindingBuilder.unmarshalBindingDefinition(uctx, sname, url); // set package and class if not validated if (!test) { // a kludge, but needed to support skipping validation ArrayList maps = bdef.getDefinitionContext().getMappings(); if (maps != null) { // set up package information from mapped class for (int i = 0; i < maps.size(); i++) { Object child = maps.get(i); if (child instanceof MappingBase) { // end scan if a real mapping is found MappingBase mapbase = (MappingBase)child; cf = mapbase.getBoundClass().getMungedFile(); if (mapbase.getBoundClass().isDirectAccess()) { break; } } } } } // set up binding root based on first mapping if (cf == null) { throw new JiBXException("One or more <mapping> elements " + "must be defined in <binding>"); } else { // get package to be used for binding classes if (tpack == null) { tpack = bdef.getDefaultPackage(); if (tpack == null) { tpack = cf.getPackage(); } } bdef.setFactoryLocation(tpack, cf.getRoot()); return bdef; } } catch (JiBXException e) { throw new JiBXException ("\n*** Error during code generation - please enter " + "a bug report for this error in Jira if the problem " + "is not listed as fixed on the online status page ***\n", e); } } else { throw new JiBXException("Binding " + fname + " is unusable because of validation errors"); } } /** * Recursively search through binding definitions for a modifiable mapped * class. This is used to determine the default package for code generation. * * @param childs * @return */ private static ClassFile findMappedClass(BindingElement root) { ArrayList childs = root.topChildren(); if (childs != null) { // recursively search for modifiable mapped class for (int i = 0; i < childs.size(); i++) { Object child = childs.get(i); if (child instanceof MappingElement) { // end scan if a real mapping is found MappingElement map = (MappingElement)child; ClassFile cf = map.getHandledClass().getClassFile(); if (!cf.isInterface() && cf.isModifiable()) { return cf; } } else if (child instanceof IncludeElement) { // recurse on included binding BindingElement bind = ((IncludeElement)child).getBinding(); if (bind != null) { ClassFile cf = findMappedClass(bind); if (cf != null) { return cf; } } } } } return null; } /** * Load binding definition from file. * * @param path file path for binding definition * @param valid validate binding flag * @return constructed binding definition * @exception IOException if error accessing file * @exception JiBXException if error in processing the binding definition */ public static BindingDefinition loadFileBinding(String path, boolean valid) throws JiBXException, IOException { // extract basic name of binding file from path String fname = fileName(path); String sname = fname; int split = sname.indexOf('.'); if (split > 0) { sname = sname.substring(0, split); } sname = convertName(sname); // construct and return the binding definition File file = new File(path); return loadBinding(fname, sname, new FileInputStream(file), file.toURL(), valid); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -