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

📄 messagewireformatxml.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 JAVA
字号:
/************************************************************************
 *
 * $Id: MessageWireFormatXML.java,v 1.8 2001/10/25 21:25:50 bondolo Exp $
 *
 * Copyright (c) 2001 Sun Microsystems, Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *       Sun Microsystems, Inc. for Project JXTA."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA"
 *    must not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact Project JXTA at http://www.jxta.org.
 *
 * 5. Products derived from this software may not be called "JXTA",
 *    nor may "JXTA" appear in their name, without prior written
 *    permission of Sun.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL SUN MICROSYSTEMS OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of Project JXTA.  For more
 * information on Project JXTA, please see
 * <http://www.jxta.org/>.
 *
 * This license is based on the BSD license adopted by the Apache Foundation.
 *********************************************************************************/

package net.jxta.impl.endpoint;

import net.jxta.endpoint.Message;
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import net.jxta.impl.document.LiteXMLDocument;
import net.jxta.impl.document.LiteXMLElement;
import net.jxta.impl.util.Base64;
import net.jxta.endpoint.MessageElement;
import net.jxta.document.MimeMediaType;
import net.jxta.document.TextElement;

/**
 * Provides static utility methods to convert Message 
 * to and from the serial format used on the wire.
 *
 * @author  chgenly
 * @version 
 */
public class MessageWireFormatXML extends MessageWireFormat {
        
    protected static final String MESSAGE_VERSION = "0";
    
    protected static final MimeMediaType outputMimeType = new MimeMediaType( "text/xml" );
    
    /**
     *  This is the mime type for this instance. It may extend the base output
     *  mimetype by use of mimetype parameters. This is needed for choice of
     *  charset for example.
     **/
    MimeMediaType type;
    
    public static class Instantiator implements MessageWireFormatFactory.Instantiator {
        
        public MimeMediaType getOutputMimeType() {
            return MessageWireFormatXML.getOutputMimeType();
        }
        
        /** Creates new MessageWireFormat */
        public MessageWireFormat newInstance( MimeMediaType type ) {
            return new MessageWireFormatXML( type );
        }
    };
    
    /**
     *  returns the mime type offered by this this wire format class.
     **/
    public static MimeMediaType getOutputMimeType() {
        return (MimeMediaType) outputMimeType.clone();
        }
    
    /** Creates new MessageWireFormat */
    public MessageWireFormatXML( MimeMediaType type ) {
        this.type = type;
    }

    public void writeMessage(OutputStream os, Message m) throws IOException
    {
        LiteXMLDocument doc = new LiteXMLDocument(new net.jxta.document.MimeMediaType("text/xml"), "Message");
        java.util.Enumeration els;

        doc.addAttribute("version", MESSAGE_VERSION);

        
        //// Write out the elements.
        els = ((MessageImpl) m).getElementsInFifoOrder();
        while(els.hasMoreElements()) {
            net.jxta.endpoint.MessageElement el = (net.jxta.endpoint.MessageElement)els.nextElement();
            writeMessageElement(doc, el);
        }
        
        doc.sendToStream(os);
    }

    /**
     * Read one message from the input stream and return it.
     * The input stream remains open.
     */
    public void readMessage(InputStream is, Message msg)
    throws IOException
    {
        LiteXMLDocument doc =  new LiteXMLDocument(new net.jxta.document.MimeMediaType("text/xml"), is);

        if (!"Message".equals(doc.getName()))
            throw new IOException("Document element is not named Message.");
        
        
        net.jxta.document.Attribute a = doc.getAttribute("version");
        if (a == null)
            throw new IOException("Missing Message version");
        String version = a.getValue();
        
        if (!version.equals(MESSAGE_VERSION))
            throw new IOException("Can not handle message version "+version+", expected "+MESSAGE_VERSION);

        java.util.Enumeration en = doc.getChildren();
        while (en.hasMoreElements()) {
            net.jxta.impl.document.LiteXMLElement el = (net.jxta.impl.document.LiteXMLElement)en.nextElement();
            readMessageElement(msg, el);
        }
    }
    
    private void readMessageElement(Message m, LiteXMLElement el) throws IOException
    {
        net.jxta.document.Attribute a;

        String name; 
        a = el.getAttribute("name");
        if (a == null) {
            throw new IOException("Missing name attribute in message element");
        }
        name = a.getValue();
        
        a = el.getAttribute("mime_type");
        if (a == null)
            throw new IOException("Missing mime_type for "+name);
        String type = a.getValue();
        
        String encoding;
        a = el.getAttribute("encoding");
        if (a == null)
            encoding = null;
        else
            encoding = a.getValue();
        
        String value = el.getTextValue();
        byte[] bytes;
        if (encoding != null && encoding.equals("base64")) {
            bytes = Base64.decodeBase64(value);
        } else {
            bytes = value.getBytes();
        }
        MessageElement mel = m.newMessageElement(name, new net.jxta.document.MimeMediaType(type), bytes, 0, bytes.length);
        m.addElement(mel);
    }
    
    private void writeMessageElement(LiteXMLDocument doc, MessageElement el)
    {
        String text;
        String encoding=null;
        byte[] bytes = el.getBytesOffset();
        int off = el.getOffset();
        int len = el.getLength();
        MimeMediaType type = el.getType();
        if (type == null)
            type = new MimeMediaType("application/octet-stream"); 
        
        if (type.getType().equals("text")) {
            text = new String(bytes, off, len);
        } else {
            text = Base64.encodeBase64(bytes);//todo: offset, len
            encoding = "base64";
        }
        LiteXMLElement xmlel = (LiteXMLElement)doc.createElement("Element", text);
        doc.appendChild(xmlel);
        xmlel.addAttribute("name", el.getName());
        if (encoding != null)
            xmlel.addAttribute("encoding", encoding);
        xmlel.addAttribute("mime_type", type.getMimeMediaType());
    }
}

⌨️ 快捷键说明

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