📄 bindinggenerator.java
字号:
* * @param uri * @return binding holder, or <code>null</code> if none */ public BindingHolder getBinding(String uri) { return (BindingHolder)m_uriBindingMap.get(uri); } /** * Get the list of binding namespace URIs. * * @return namespaces */ public ArrayList getNamespaces() { return m_uriBindingMap.keyList(); } /** * Check if a character is an ASCII alpha character. * * @param chr * @return alpha character flag */ private static boolean isAsciiAlpha(char chr) { return (chr >= 'a' && chr <= 'z') || (chr >= 'A' && chr <= 'Z'); } /** * Check if a character is an ASCII numeric character. * * @param chr * @return numeric character flag */ private static boolean isAsciiNum(char chr) { return chr >= '0' && chr <= '9'; } /** * Check if a character is an ASCII alpha or numeric character. * * @param chr * @return alpha or numeric character flag */ private static boolean isAsciiAlphaNum(char chr) { return isAsciiAlpha(chr) || isAsciiNum(chr); } /** * Configure the names to be used for writing bindings to files. If only one * binding has been defined, it just gets the supplied name. If multiple * bindings have been defined, a single root binding is constructed which * includes all the other bindings, and that root binding is given the * supplied names while the other bindings are given unique names within the * same directory. * * @param name file name for root or singleton binding definition * @return root or singleton binding holder */ public BindingHolder configureBindings(String name) { BindingHolder rhold; BindingElement root; ArrayList uris = getNamespaces(); if (uris.size() == 1) { // single binding, just write it using supplied name rhold = (BindingHolder)m_uriBindingMap.get(uris.get(0)); root = rhold.getBinding(); } else { // get or create no namespace binding rhold = findBinding(null); root = rhold.getBinding(); // set file names and add to root binding Set nameset = new HashSet(); for (int i = 0; i < uris.size(); i++) { String uri = (String)uris.get(i); BindingHolder holder = (BindingHolder)m_uriBindingMap.get(uri); if (holder != rhold) { // get last part of namespace URI as file name candidate String bindname; String raw = holder.getNamespace(); if (raw == null) { bindname = "nonamespaceBinding"; } else { int split = raw.lastIndexOf(':'); raw = raw.substring(split+1); split = raw.lastIndexOf('/'); raw = raw.substring(split+1); split = raw.lastIndexOf('\\'); raw = raw.substring(split+1); // eliminate any invalid characters in name StringBuffer buff = new StringBuffer(); int index = 0; char chr = raw.charAt(0); if (isAsciiAlpha(chr)) { buff.append(chr); index = 1; } else { buff.append('_'); } while (index < raw.length()) { chr = raw.charAt(index++); if (isAsciiAlphaNum(chr)) { buff.append(chr); } } buff.append("Binding"); bindname = buff.toString(); } // ensure uniqueness of the name String uname = bindname.toLowerCase(); int pass = 0; while (nameset.contains(uname)) { bindname = bindname + pass; uname = bindname.toLowerCase(); } nameset.add(uname); holder.setFileName(bindname + ".xml"); // include within the root binding IncludeElement include = new IncludeElement(); include.setIncludePath(holder.getFileName()); root.addTopChild(include); } } } // set the file name on the singleton or root binding rhold.setFileName(name); // set the binding name based on the file name int split = name.lastIndexOf('.'); if (split > 0) { root.setName(name.substring(0, split)); } else { root.setName(name); } // set the target package for code generation root.setTargetPackage(m_targetPackage); return rhold; } /** * Write the bindings to supplied destination path. * * @param dir target directory for writing binding definitions, output to * console if <code>null</code> * @throws JiBXException * @throws IOException */ public void writeBindings(File dir) throws JiBXException, IOException { // write the bindings IBindingFactory fact = BindingDirectory.getFactory(BindingElement.class); IMarshallingContext ictx = fact.createMarshallingContext(); ictx.setIndent(2); ArrayList uris = getNamespaces(); for (int i = 0; i < uris.size(); i++) { String uri = (String)uris.get(i); BindingHolder holder = (BindingHolder)m_uriBindingMap.get(uri); if (dir == null) { ictx.setOutput(System.out, null); } else { File file = new File(dir, holder.getFileName()); ictx.setOutput(new FileOutputStream(file), null); } ((IMarshallable)holder.getBinding()).marshal(ictx); ictx.getXmlWriter().flush(); } } /** * Run the binding generation using command line parameters. * * @param args * @throws JiBXException * @throws IOException */ public static void main(String[] args) throws JiBXException, IOException { BindingGeneratorCommandLine parms = new BindingGeneratorCommandLine(); if (args.length > 0 && parms.processArgs(args)) { // generate bindings for all namespaces BindingGenerator gen = new BindingGenerator(parms.getGlobal()); gen.generate(parms.getAbstract(), parms.getExtraArgs()); // write the bindings gen.configureBindings(parms.getBindingName()); gen.writeBindings(parms.getGeneratePath()); } else { if (args.length > 0) { System.err.println("Terminating due to command line errors"); } else { parms.printUsage(); } System.exit(1); } } /** * Holder for the details of how a class is going to be mapped. This * information is needed in order to determine how to reference the mapped * class when it's used within another mapping definition. */ public static class MappingDetail { /** Generate abstract mapping flag. */ private boolean m_useAbstract; /** Generate concrete mapping flag. */ private boolean m_useConcrete; /** Flag for class extended by other mapped class(es). */ private boolean m_isExtended; /** Abstract mapping type name (<code>null</code> if none). */ private final QName m_typeQName; /** Concrete mapping element name (<code>null</code> if none). */ private QName m_elementQName; /** Concrete mapping type extended by this one. */ private String m_extendsType; /** Flag for mapping(s) has been generated. */ private boolean m_isGenerated; /** Abstract mapping definition (<code>null</code> if none). */ private MappingElement m_abstractMapping; /** Concrete mapping definition (<code>null</code> if none). */ private MappingElement m_concreteMapping; /** * Constructor. * * @param aname abstract mapping type name * @param cname concrete mapping element name * @param stype superclass for extension (<code>null</code> if not an * extension mapping) */ protected MappingDetail(QName aname, QName cname, String stype) { m_typeQName = aname; m_elementQName = cname; m_extendsType = stype; } public MappingElement getAbstractMapping() { return m_abstractMapping; } public void setAbstractMapping(MappingElement abstractMapping) { m_abstractMapping = abstractMapping; } public MappingElement getConcreteMapping() { return m_concreteMapping; } public void setConcreteMapping(MappingElement concreteMapping) { m_concreteMapping = concreteMapping; } public boolean isExtended() { return m_isExtended; } public void setExtended(boolean isExtended) { m_isExtended = isExtended; } public boolean isGenerated() { return m_isGenerated; } public void setGenerated(boolean isGenerated) { m_isGenerated = isGenerated; } public boolean isUseAbstract() { return m_useAbstract; } public void setUseAbstract(boolean useAbstract) { m_useAbstract = useAbstract; } public boolean isUseConcrete() { return m_useConcrete; } public void setUseConcrete(boolean useConcrete) { m_useConcrete = useConcrete; } public QName getElementQName() { return m_elementQName; } public void setElementQName(QName elementQName) { m_elementQName = elementQName; } public String getExtendsType() { return m_extendsType; } public QName getTypeQName() { return m_typeQName; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -