jtxmlhelper.java

来自「Java Pattern Oriented Framework (Jt) 是为了」· Java 代码 · 共 1,383 行 · 第 1/3 页

JAVA
1,383
字号
            try {
                obj = new Character (value.charAt(0));
                return (obj);    
            } catch (Exception e) {
                handleException (e);
                return (null);
            }             

        }

        if (classname.equals ("java.lang.String")) {

            try {
                obj = new String (value);
                return (obj);	   
            } catch (Exception e) {
                handleException (e);
                return (null);
            }             

        }

        if (classname.equals ("java.lang.Integer")) {

            try {
                obj = new Integer (value);
                return (obj);	   
            } catch (Exception e) {
                handleException (e);
                return (null);
            }             

        }

        if (classname.equals ("java.lang.Long")) {

            try {
                obj = new Long (value);
                return (obj);	   
            } catch (Exception e) {
                handleException (e);
                return (null);
            }             

        }

        if (classname.equals ("java.lang.Boolean")) {

            try {
                obj = new Boolean (value);
                return (obj);	   
            } catch (Exception e) {
                handleException (e);
                return (null);
            }             

        }

        handleError ("createInstance: unable to handle class type:" +
                classname);
        return (null); // check
    }


    /** 
     * End element (SAX API). 
     */

    public void endElement(String uri,
            String name,
            String qName)
    throws SAXException {
        JtMessage msg;
        int i;
        JtXMLContext parentCtx = null, currentCtx;
        Vector children;
        HashMap map;
        StringBuffer buffer;
        Object newObject;


        if (qName == null)
            return;  //check


        handleTrace ("End element: " + qName, JtObject.JtMIN_LOG_LEVEL);

        if ("Object".equals (qName)) { //check

//          if (map == null)
//          return; // check

            newObject = createInstance ();
            currentObject = newObject;


            //handleTrace ("End element: "  + newObject.getClass().getName());

            currentCtx = (JtXMLContext) contextStack.pop ();
            currentCtx.setObject(newObject);

            if (newObject instanceof JtCollection || newObject instanceof JtList) {
                children = (Vector) currentCtx.getChildren();
                if (children == null)
                    return;

                for (i = 0; i < children.size (); i++) {
                    msg = new JtMessage (JtCollection.JtADD); // chech message ID
                    msg.setMsgContent (children.elementAt (i));
                    ((JtObject) newObject).processMessage (msg);
                    //sendMessage (object, msg);
                }
                //return;
            }

            if (contextStack.isEmpty()) {
                return; // check
            }


            parentCtx = (JtXMLContext) contextStack.peek();

            if (!("Object".equals(parentCtx.getElement()))) {
                //handleTrace ("End Element: this attribute references an object " +  
                //        newObject.getClass().getName());
                parentCtx.setChild(newObject);
                return;
            }   

            //handleTrace ("End Element: adding child to parent " +  
            //        newObject.getClass().getName());

            children = (Vector) parentCtx.getChildren ();             
            if (children == null) {
                children = new Vector ();
                parentCtx.setChildren(children);
                children.addElement (newObject);
            } else {
                children.addElement (newObject);                 
            }

            //return;
        } else { 


            currentCtx = (JtXMLContext) contextStack.pop();

            parentCtx = (JtXMLContext) contextStack.peek();
            map = (HashMap) parentCtx.getProperties();


            if (currentCtx.getChild() != null) {
                map.put (qName, currentCtx.getChild());
                return;
            }    

            buffer = currentCtx.getBuffer();

            handleTrace (qName + ":" + buffer, JtObject.JtMIN_LOG_LEVEL);

            if (buffer != null)
                map.put (qName, buffer.toString ());

        } 


    }


    /** 
     * Start document (SAX API). 
     */
    public void startDocument() throws SAXException {

    } 

    /** 
     * End document (SAX API). 
     */

    public void endDocument()
    throws SAXException {

    }

    /** 
     * Characters (SAX API). 
     */

    public void characters(char[] ch,
            int start,
            int length)
    throws SAXException {
        //String tmp;
        JtXMLContext ctx;
        StringBuffer buffer;

        ctx = (JtXMLContext) contextStack.peek();

        if (ctx == null)
            return; // check


        //handleTrace ("Characters:    \"");
        if (ctx.getBuffer() == null) {
            buffer = new StringBuffer ();
            ctx.setBuffer(buffer);
        } else
            buffer = ctx.getBuffer();

        for (int i = start; i < start + length; i++) {
            buffer.append (ch[i]);

            /*
      switch (ch[i]) {
        case '\\':
          handleTrace("\\\\");
          break;
	case '"':
	  handleTrace("\\\"");
	  break;
	case '\n':
	  //handleTrace("\\n");
	  break;
	case '\r':
          handleTrace ("\\r");
          break;
        case '\t':
          handleTrace ("\\t");
          break;
        default:
          buffer.append (ch[i]);
          break;
      }
             */
        }

        //handleTrace("\"\n");
        //elemValue = buffer.toString ();
        buffer.toString().trim ();

        //if (!tmp.equals ("")) {
        //  handleTrace("JtXMLMsgReader.characters:" + 
        //    buffer);      
        //}    

    }

    /** 
     * endPrefixMapping - SAX API. 
     */
    public void endPrefixMapping(String prefix)
    throws SAXException {

    }

    /** 
     * ignorableWhitespace - SAX API. 
     */
    public void ignorableWhitespace(char[] ch,
            int start,
            int length)
    throws SAXException {

    }

    /** 
     * processingInstruction - SAX API. 
     */
    public void processingInstruction(String target,
            String data)
    throws SAXException {

    }

    /** 
     * setDocumentLocator - SAX API. 
     */

    public void setDocumentLocator(Locator locator) {

    }

    /** 
     * skippedEntity - SAX API. 
     */

    public void skippedEntity(java.lang.String name)
    throws SAXException {

    }


    /** 
     * startPrefixMapping - SAX API. 
     */

    public void startPrefixMapping(String prefix,
            String uri)
    throws SAXException
    {

    }

    // Realize helper

    void realizeHelper() {

        if (reader != null)
            return;

        try{
            reader = XMLReaderFactory.createXMLReader (parserName);

            reader.setContentHandler (this);
            //reader.setErrorHandler (this);
        } catch (Exception ex) {

            handleException (ex);
        }

    }

    private Object convertXMLToObject (String string) {

        ByteArrayInputStream bstream;

        currentObject = null;  // check

        if (string == null)
            return (null);

        if (reader == null)
            realizeHelper();


        try {

            bstream = new ByteArrayInputStream (string.getBytes());     
            reader.parse (new InputSource ((InputStream) bstream));

        } catch (Exception ex) {
            if ("XMLEncoded".equals(ex.getMessage ())) 
                currentObject = decodeObject (string);  // the object has been encoded using XMLEncode
            else  
                handleException (ex);

        }
        return currentObject;
    }  

    // Convert Jt Object

    private String convertJtObjectToXML (Object obj) {

        StringBuffer buf = new StringBuffer ();

        Hashtable tbl;  
        //Iterator it; 
        String key;
        Enumeration keys;

        if (obj == null)
            return (null);

        tbl = getAttributes (obj); 

        if (tbl == null)
            return (null);

//      it = tbl.keys().iterator (); 


//      buf.append ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
        buf.append ("<Object>\n");
        buf.append ("<classname>" + obj.getClass().getName() + "</classname>\n");


        if (tbl != null) {
            keys = tbl.keys ();    
            while (keys.hasMoreElements()) {

                key = (String) keys.nextElement ();

                buf.append ("<" + key + ">");
                buf.append ((String) tbl.get (key));  //check
                buf.append ("</" + key + ">\n");


            }
        }
        buf.append ("</Object>\n"); 

        //handleTrace ("convertJtObjectToXML (XML):" + buf);
        return (buf.toString ());

    }


    // Convert Jt Collection

    private String convertJtCollectionToXML (Object obj) {

        StringBuffer buf = new StringBuffer ();
        JtIterator jit;
        Object tmp;
        String tmp1;

        if (obj == null)
            return (null);

//      buf.append ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
        buf.append ("<Object>\n");
        buf.append ("<classname>" + obj.getClass().getName() + "</classname>\n");

        jit = (JtIterator) getValue (obj, "iterator");

        /*
    if (jit == null)
      return (null);
         */  

        if (jit != null)
            for (;;) {
                //tmp = sendMessage (jit, new JtMessage ("JtNEXT"));
                tmp = jit.processMessage (new JtMessage (JtIterator.JtNEXT));    
                if (tmp == null)
                    break;
                tmp1 = convertToXML (tmp);

                if (tmp1 == null)
                    return (null); // check

                buf.append (tmp1);
            }  

        buf.append ("</Object>\n"); 

        return (buf.toString ());

    }


    private String stackTrace (JtRemoteException ex ) {
        ByteArrayOutputStream bstream;


        if (ex == null)
            return (null);

        bstream = new ByteArrayOutputStream ();

        if (ex.getTrace() != null)
            return ex.getTrace();

        //handleError ("hello");


        //ex = (Exception) getValue (this, "objException");

        ex.printStackTrace (new PrintWriter (bstream, true));

⌨️ 快捷键说明

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