⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 marshallingcontext.java

📁 对xml很好的java处理引擎,编译中绑定xml
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
     */
    
    public IMarshaller getMarshaller(int index, String name)
        throws JiBXException {
        if (index >= m_classes.length || !name.equals(m_classes[index])) {
            throw new JiBXException("Marshalling not defined for class " +
                name);
        }
        if (m_marshallers[index] == null) {
            
            // load the marshaller class and create an instance
            String mname = m_marshallerClasses[index];
            if (mname == null) {
                throw new JiBXException("No marshaller defined for class " +
                    name);
            }
            try {
                
                // first try loading class from binding factory class loader
                Class clas = null;
                ClassLoader factldr = null;
                if (m_factory != null) {
                    factldr = m_factory.getClass().getClassLoader();
                    try {
                        clas = factldr.loadClass(mname);
                    } catch (ClassNotFoundException e) { /* fall through */ }
                }
                if (clas == null) {
                    
                    // next try the context class loader, if set
                    ClassLoader ctxldr =
                        Thread.currentThread().getContextClassLoader();
                    if (ctxldr != null) {
                        try {
                            clas = ctxldr.loadClass(mname);
                        } catch (ClassNotFoundException e) { /* fall through */ }
                    }
                    if (clas == null) {
                        
                        // not found, try the loader that loaded this class
                        ClassLoader thisldr =
                            MarshallingContext.class.getClassLoader();
                        if (thisldr != factldr && thisldr != ctxldr) {
                            try {
                                clas = thisldr.loadClass(mname);
                            } catch (ClassNotFoundException e)
                                { /* fall through */ }
                        }
                    }
                }
                if (clas == null) {
                    throw new JiBXException("Unable to load marshaller class " +
                        mname);
                }
                
                // create an instance of marshaller class
                IMarshaller m = (IMarshaller)clas.newInstance();
                m_marshallers[index] = m;
                
            } catch (JiBXException e) {
                throw e;
            } catch (Exception e) {
                throw new JiBXException
                    ("Unable to create marshaller of class " + mname + ":",
                    e);
            }
        }
        return m_marshallers[index];
    }
    
    /**
     * Marshal document from root object. This internal method just verifies
     * that the object is marshallable, then calls the marshal method on the
     * object itself.
     *
     * @param root object at root of structure to be marshalled, which must have
     * a top-level mapping in the binding
     * @throws JiBXException on any error (possibly wrapping other exception)
     */

    protected void marshalRoot(Object root) throws JiBXException {
        if (root instanceof IMarshallable) {
            boolean valid = false;
            IMarshallable mable = (IMarshallable)root;
            int index = mable.JiBX_getIndex();
            if (index < m_classes.length) {
                String cname = m_classes[index];
                Class mclass = mable.getClass();
                while (!mclass.getName().equals("java.lang.Object")) {
                    if (mclass.getName().equals(cname)) {
                        valid = true;
                        break;
                    } else {
                        mclass = mclass.getSuperclass();
                    }
                }
            }
            if (valid) {
                mable.marshal(this);
            } else {
                throw new JiBXException("Supplied root object of class " +
                    root.getClass().getName() +
                    " is incompatible with the binding used for this context");
            }
        } else {
            throw new JiBXException("Supplied root object of class " +
                root.getClass().getName() +
                " cannot be marshalled without top-level mapping");
        }
    }
    
    /**
     * Marshal document from root object without XML declaration. This can only
     * be validly called immediately following one of the set output methods;
     * otherwise the output document will be corrupt. The effect of this method
     * is the same as the sequence of a call to marshal the root object using
     * this context followed by a call to {@link #endDocument}.
     *
     * @param root object at root of structure to be marshalled, which must have
     * a top-level mapping in the binding
     * @throws JiBXException on any error (possibly wrapping other exception)
     */

    public void marshalDocument(Object root) throws JiBXException {
        marshalRoot(root);
        endDocument();
    }
    
    /**
     * Marshal document from root object. This can only be validly called
     * immediately following one of the set output methods; otherwise the output
     * document will be corrupt. The effect of this method is the same as the
     * sequence of a call to {@link #startDocument}, a call to marshal the root
     * object using this context, and finally a call to {@link #endDocument}.
     *
     * @param root object at root of structure to be marshalled, which must have
     * a top-level mapping in the binding
     * @param enc document encoding, <code>null</code> if not specified
     * @param alone standalone document flag, <code>null</code> if not
     * specified
     * @throws JiBXException on any error (possibly wrapping other exception)
     */

    public void marshalDocument(Object root, String enc, Boolean alone)
        throws JiBXException {
        startDocument(enc, alone);
        marshalRoot(root);
        endDocument();
    }
    
    /**
     * Marshal document from root object to output stream with encoding. The
     * effect of this method is the same as the sequence of a call to {@link
     * #startDocument}, a call to marshal the root object using this context,
     * and finally a call to {@link #endDocument}.
     *
     * @param root object at root of structure to be marshalled, which must have
     * a top-level mapping in the binding
     * @param enc document encoding, <code>null</code> if not specified
     * @param alone standalone document flag, <code>null</code> if not
     * specified
     * @param outs stream for document data output
     * @throws JiBXException on any error (possibly wrapping other exception)
     */

    public void marshalDocument(Object root, String enc, Boolean alone,
        OutputStream outs) throws JiBXException {
        startDocument(enc, alone, outs);
        marshalRoot(root);
        endDocument();
    }
    
    /**
     * Marshal document from root object to writer. The effect of this method
     * is the same as the sequence of a call to {@link #startDocument}, a call
     * to marshal the root object using this context, and finally a call to
     * {@link #endDocument}.
     *
     * @param root object at root of structure to be marshalled, which must have
     * a top-level mapping in the binding
     * @param enc document encoding, <code>null</code> if not specified
     * @param alone standalone document flag, <code>null</code> if not
     * specified
     * @param outw writer for document data output
     * @throws JiBXException on any error (possibly wrapping other exception)
     */

    public void marshalDocument(Object root, String enc, Boolean alone,
        Writer outw) throws JiBXException {
        startDocument(enc, alone, outw);
        marshalRoot(root);
        endDocument();
    }
    
    /**
     * Get shared ID map. The ID map returned is not used directly by the
     * marshalling code, but is provided to support user extensions.
     *
     * @return ID map
     */
    
    public HashMap getIdMap() {
        if (m_idMap == null) {
            m_idMap = new HashMap();
        }
        return m_idMap;
    }
    
    /**
     * Set a user context object. This context object is not used directly by
     * JiBX, but can be accessed by all types of user extension methods. The
     * context object is automatically cleared by the {@link #reset()} method,
     * so to make use of this you need to first call the appropriate version of
     * the <code>setOutput()</code> method, then this method, and finally one of
     * the <code>marshalDocument</code> methods which uses the previously-set
     * output (not the ones which take a stream or writer as parameter, since
     * they call <code>setOutput()</code> themselves).
     * 
     * @param obj user context object, or <code>null</code> if clearing existing
     * context object
     * @see #getUserContext()
     */
    public void setUserContext(Object obj) {
        m_userContext = obj;
    }
    
    /**
     * Get the user context object.
     * 
     * @return user context object, or <code>null</code> if no context object
     * set
     * @see #setUserContext(Object)
     */
    public Object getUserContext() {
        return m_userContext;
    }

    /**
     * Push created object to marshalling stack. This must be called before
     * beginning the marshalling of the object. It is only called for objects
     * with structure, not for those converted directly to and from text.
     *
     * @param obj object being marshalled
     */

    public void pushObject(Object obj) {
        int depth = m_stackDepth;
        if (depth >= m_objectStack.length) {
            Object[] stack = new Object[depth*2];
            System.arraycopy(m_objectStack, 0, stack, 0, depth);
            m_objectStack = stack;
        }
        m_objectStack[depth] = obj;
        m_stackDepth++;
    }

    /**
     * Pop marshalled object from stack.
     *
     * @throws JiBXException if no object on stack
     */

    public void popObject() throws JiBXException {
        if (m_stackDepth > 0) {
            --m_stackDepth;
        } else {
            throw new JiBXException("No object on stack");
        }
    }
    
    /**
     * Get current marshalling object stack depth. This allows tracking
     * nested calls to marshal one object while in the process of
     * marshalling another object. The bottom item on the stack is always the
     * root object being marshalled.
     *
     * @return number of objects in marshalling stack
     */

    public int getStackDepth() {
        return m_stackDepth;
    }
    
    /**
     * Get object from marshalling stack. This stack allows tracking nested
     * calls to marshal one object while in the process of marshalling
     * another object. The bottom item on the stack is always the root object
     * being marshalled.
     *
     * @param depth object depth in stack to be retrieved (must be in the range
     * of zero to the current depth minus one).
     * @return object from marshalling stack
     */

    public Object getStackObject(int depth) {
        return m_objectStack[m_stackDepth-depth-1];
    }
    
    /**
     * Get top object on marshalling stack. This is safe to call even when no
     * objects are on the stack.
     *
     * @return object from marshalling stack, or <code>null</code> if none
     */

    public Object getStackTop() {
        if (m_stackDepth > 0) {
            return m_objectStack[m_stackDepth-1];
        } else {
            return null;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -