📄 bindingdefinition.java
字号:
* Set ID property. This parent binding component interface method should * never be called for the binding definition, and will throw a runtime * exception if it is called. * * @param child child defining the ID property * @return <code>false</code> */ public boolean setIdChild(IComponent child) { throw new IllegalStateException("Internal error - setIdChild for root"); } /** * Get default package used for code generation. * * @return default code generation package */ public String getDefaultPackage() { return m_targetPackage; } /** * Get root directory for default code generation package. * * @return root for default code generation */ public File getDefaultRoot() { return m_targetRoot; } /** * Set location for binding factory class generation. * * @param tpack target package for generated context factory * @param root target root for generated context factory */ public void setFactoryLocation(String tpack, File root) { m_targetPackage = tpack; m_targetRoot = root; } /** * Get index number of binding. * * @return index number for this binding definition */ public int getIndex() { return m_index; } /** * Check if binding is defined for unmarshalling. * * @return <code>true</code> if defined, <code>false</code> if not */ public boolean isInput() { return m_isInput; } /** * Check if binding is defined for marshalling. * * @return <code>true</code> if defined, <code>false</code> if not */ public boolean isOutput() { return m_isOutput; } /** * Check if global ids are used by binding. * * @return <code>true</code> if defined, <code>false</code> if not */ public boolean isIdGlobal() { return m_isIdGlobal; } /** * Check if forward ids are supported by unmarshalling binding. * * @return <code>true</code> if supported, <code>false</code> if not */ public boolean isForwards() { return m_isForwards; } /** * Check if source tracking is supported by unmarshalling binding. * * @return <code>true</code> if defined, <code>false</code> if not */ public boolean isTrackSource() { return m_isTrackSource; } /** * Check if default constructor generation is enabled. * * @return <code>true</code> if default constructor generation enabled, * <code>false</code> if not */ public boolean isAddConstructors() { return m_isAddConstructors; } /** * Get prefix for method or class generation. * * @return prefix for names created by this binding */ public String getPrefix() { return GENERATE_PREFIX + m_name; } /** * Get index for mapped class from binding. If the class is not already * included in any binding it is first added to the list of bound classes. * All bindings use the same index numbers to allow easy lookup of the * appropriate marshaller and unmarshaller within a particular binding, but * this does mean that all bindings dealing with a common set of classes * need to be compiled together. This uses the same sequence of values as * the {@link #getMarshallerUnmarshallerIndex} method but differs in that * the values returned by this method are unique per class. This method is * intended for use with <mapping> definitions. It is an error to call * this method after calling the {@link #getMarshallerUnmarshallerIndex} * method. * * @param name fully qualified name of mapped class * @return index number of class */ public int getMappedClassIndex(String name) { if (m_isMappedDone) { throw new IllegalStateException ("Internal error: Call out of sequence"); } else { return s_mappedClasses.findOrAdd(name); } } /** * Get marshaller/unmarshaller slot index in binding. This uses the same * sequence of values as the {@link #getMappedClassIndex} method but differs * in that the same class may have more than one marshaller/unmarshaller * slot defined. It's intended for user-defined marshallers/unmarshallers * where use is specific to a particular context. After the slot has been * assigned by this method, the {@link #setMarshallerUnmarshallerClasses} * method must be used to set the actual class names. * * @param clas fully qualified name of class handled by * marshaller/unmarshaller * @return slot number for marshaller/unmarshaller */ public int getMarshallerUnmarshallerIndex(String clas) { if (!m_isMappedDone) { m_isMappedDone = true; m_mumIndex = s_mappedClasses.size(); m_extraClasses = new ArrayList(); m_extraMarshallers = new ArrayList(); m_extraUnmarshallers = new ArrayList(); } m_extraClasses.add(clas); m_extraMarshallers.add(null); m_extraUnmarshallers.add(null); return m_mumIndex++; } /** * Set marshaller and unmarshaller class names for slot. * * @param slot assigned marshaller/unmarshaller slot number * @param mclas fully qualified name of marshaller class * @param uclas fully qualified name of unmarshaller class */ public void setMarshallerUnmarshallerClasses(int slot, String mclas, String uclas) { int index = slot - s_mappedClasses.size(); m_extraMarshallers.set(index, mclas); m_extraUnmarshallers.set(index, uclas); } /** * Get index for ID'ed class from binding. If the class is not already * included it is first added to the binding. If globally unique IDs are * used this always returns <code>0</code>. * * @param name fully qualified name of ID'ed class * @return index number of class */ public int getIdClassIndex(String name) { if (m_isIdGlobal) { return 0; } else { if (m_uniqueIds == null) { m_uniqueIds = new ArrayMap(); } return m_uniqueIds.findOrAdd(name); } } /** * Get index for namespace URI in binding. If the URI is not already * included it is first added to the binding. The empty namespace URI * is always given index number <code>0</code>. * * @param uri namespace URI to be included in binding * @param prefix prefix used with namespace * @return index number of namespace */ public int getNamespaceUriIndex(String uri, String prefix) { int index = m_namespaceUris.findOrAdd(uri); if (index > m_namespacePrefixes.size()) { m_namespacePrefixes.add(prefix); } return index; } /** * Set flag for schema instance namespace used in binding. */ public void setSchemaInstanceUsed() { m_isSchemaInstanceUsed = true; } /** * Generate code. First sets linkages and executes code generation for * each top-level mapping defined in this binding, which in turn propagates * the code generation all the way down. Then generates the actual binding * factory for this binding. * * TODO: handle unidirectional bindings properly * * @param verbose flag for verbose output * @throws JiBXException if error in code generation */ public void generateCode(boolean verbose) throws JiBXException { // check schema instance namespace usage if (m_isSchemaInstanceUsed) { NamespaceDefinition xsins = NamespaceDefinition.buildNamespace ("http://www.w3.org/2001/XMLSchema-instance", "xsi"); ArrayList mappings = m_activeContext.getMappings(); for (int i = 0; i < mappings.size(); i++) { Object mapping = mappings.get(i); if (mapping instanceof MappingDefinition) { ((MappingDefinition)mapping).addNamespace(xsins); } } } // handle basic linkage and child code generation BoundClass.setModify(m_targetRoot, m_targetPackage); m_activeContext.linkMappings(); m_activeContext.setLinkages(); m_activeContext.generateCode(verbose, m_isForceClasses); // disabled because of potential recursion issues if (verbose) { System.out.println("After linking view of binding " + m_name + ':'); print(); } // build the binding factory class String name; if (m_targetPackage.length() == 0) { name = getPrefix() + FACTORY_SUFFIX; } else { name = m_targetPackage + '.' + getPrefix() + FACTORY_SUFFIX; } ClassFile base = ClassCache.getClassFile("java.lang.Object"); ClassFile cf = new ClassFile(name, m_targetRoot, base, Constants.ACC_PUBLIC, FACTORY_INTERFACES); // add static field for instance and member fields for data ClassItem inst = cf.addField(FACTORY_INTERFACE, FACTORY_INSTNAME, FACTORY_INSTACCESS); ClassItem marshs = cf.addPrivateField(STRING_ARRAYTYPE, MARSHALLER_ARRAYNAME); ClassItem umarshs = cf.addPrivateField(STRING_ARRAYTYPE, UNMARSHALLER_ARRAYNAME); ClassItem classes = cf.addPrivateField(STRING_ARRAYTYPE, CLASSES_ARRAYNAME); ClassItem uris = cf.addPrivateField(STRING_ARRAYTYPE, URIS_ARRAYNAME); ClassItem prefs = cf.addPrivateField(STRING_ARRAYTYPE, PREFIXES_ARRAYNAME); ClassItem gnames = cf.addPrivateField(STRING_ARRAYTYPE, GNAMES_ARRAYNAME); ClassItem guris = cf.addPrivateField(STRING_ARRAYTYPE, GURIS_ARRAYNAME); ClassItem idnames = cf.addPrivateField(STRING_ARRAYTYPE, IDNAMES_ARRAYNAME); // add the private constructor method MethodBuilder mb = new ExceptionMethodBuilder("<init>", Type.VOID, new Type[0], cf, Constants.ACC_PRIVATE); // call the superclass constructor mb.appendLoadLocal(0); mb.appendCallInit("java.lang.Object", "()V"); // create and fill array of unmarshaller class names int count = s_mappedClasses.size(); int mcnt = m_isMappedDone ? m_mumIndex : count; if (m_isInput) { mb.appendLoadLocal(0); mb.appendLoadConstant(mcnt); mb.appendCreateArray("java.lang.String"); for (int i = 0; i < count; i++) { String cname = (String)s_mappedClasses.get(i); IMapping map = m_activeContext.getMappingAtLevel(cname); if (map != null && map.getUnmarshaller() != null) { mb.appendDUP(); mb.appendLoadConstant(i); mb.appendLoadConstant(map.getUnmarshaller().getName()); mb.appendAASTORE(); } } for (int i = count; i < mcnt; i++) { mb.appendDUP(); mb.appendLoadConstant(i); mb.appendLoadConstant ((String)m_extraUnmarshallers.get(i-count)); mb.appendAASTORE(); } mb.appendPutField(umarshs); } // create and fill array of marshaller class names if (m_isOutput) { mb.appendLoadLocal(0); mb.appendLoadConstant(mcnt); mb.appendCreateArray("java.lang.String"); for (int i = 0; i < count; i++) { String cname = (String)s_mappedClasses.get(i); IMapping map = m_activeContext.getMappingAtLevel(cname); if (map != null && map.getMarshaller() != null) { mb.appendDUP(); mb.appendLoadConstant(i); mb.appendLoadConstant(map.getMarshaller().getName()); mb.appendAASTORE(); } } for (int i = count; i < mcnt; i++) { mb.appendDUP(); mb.appendLoadConstant(i); mb.appendLoadConstant((String)m_extraMarshallers.get(i-count)); mb.appendAASTORE(); } mb.appendPutField(marshs); } // create and fill array of mapped class names mb.appendLoadLocal(0); mb.appendLoadConstant(mcnt); mb.appendCreateArray("java.lang.String"); for (int i = 0; i < count; i++) { mb.appendDUP(); mb.appendLoadConstant(i); mb.appendLoadConstant((String)s_mappedClasses.get(i)); mb.appendAASTORE(); } for (int i = count; i < mcnt; i++) { mb.appendDUP(); mb.appendLoadConstant(i); mb.appendLoadConstant((String)m_extraClasses.get(i-count)); mb.appendAASTORE(); } mb.appendPutField(classes); // create and fill array of namespace URIs if (m_isOutput) { mb.appendLoadLocal(0); mb.appendLoadConstant(m_namespaceUris.size()); mb.appendCreateArray("java.lang.String"); for (int i = 0; i < m_namespaceUris.size(); i++) { mb.appendDUP(); mb.appendLoadConstant(i);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -