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

📄 advertisementutilities.java

📁 jxta_src_2.41b jxta 2.41b 最新版源码 from www.jxta.org
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * 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. * * $Id: AdvertisementUtilities.java,v 1.11 2006/06/19 18:27:35 bondolo Exp $ */package net.jxta.util;import net.jxta.platform.*;import net.jxta.document.*;import net.jxta.exception.*;import net.jxta.protocol.*;import net.jxta.id.*;import net.jxta.discovery.*;import net.jxta.peergroup.*;import net.jxta.pipe.*;import net.jxta.util.*;import net.jxta.endpoint.*;import java.io.*;import java.net.*;import java.util.*;/** *  @deprecated Will be deprecated soon. Do not use these methods. They contain *  a number of incorrect assumption that cannot be corrected while maintaining *  backwards compatibility with programs which already use them. **/public final class AdvertisementUtilities {    public static final StructuredTextDocument STANDARD_COMPATABILITY =  (StructuredTextDocument) StructuredDocumentFactory.newStructuredDocument(MimeMediaType.XMLUTF8, "Comp");    public static final String STANDARD_URI = "http://www.jxta.org/download/jxta.jar";    public static final String STANDARD_PROVIDER = "jxta.org";        static {        Element element = STANDARD_COMPATABILITY.createElement("Efmt", "JDK1.4.1");        STANDARD_COMPATABILITY.appendChild(element);        element = STANDARD_COMPATABILITY.createElement("Bind", "V2.0 Ref Impl");        STANDARD_COMPATABILITY.appendChild(element);    }        private AdvertisementUtilities() {}        /**     *  Read a JXTA Advertisement from a File     *     * @param fileName The file containing the Advertisement     * @return An polymorphic Advertisement object     * @throws JxtaException if Unable to parse the Advertisement     **/    public static Advertisement readAdvertisementFromFile(String fileName) throws JxtaException {        return readAdvertisementFromFile(new File(fileName));    }        /**     *  Read a JXTA Advertisement from a File     *     * @param file The file containing the Advertisement     * @return An polymorphic Advertisement object     * @throws JxtaException if Unable to parse the Advertisement     **/    public static Advertisement readAdvertisementFromFile(File file) throws JxtaException  {        FileInputStream in = null;                try {            in = new FileInputStream(file);                        return AdvertisementFactory.newAdvertisement(MimeMediaType.XML_DEFAULTENCODING, in);        } catch (IOException e) {            throw new JxtaException("Advertisement Load Failed: " + file, e);        } catch (Exception e) {            throw new JxtaException("Advertisement Load Failed: " + file, e);        } finally {            if (in != null) {                try {                    in.close();                } catch (IOException ignored) { ; }            }        }    }        /**     *  Save a JXTA Advertisement as an XML Document to a File     *     * @param adv The Advertisement to be saved     * @param fileName The file to store the Advertisement     * @throws JxtaException if Unable to parse the Advertisement     **/    public static void saveAdvertisementToFile(Advertisement adv, String fileName) throws JxtaException {        saveAdvertisementToFile(adv, new File(fileName));    }        /**     *  Save a JXTA Advertisement as an XML Document to a File     *     * @param adv The Advertisement to be saved     * @param file The file to store the Advertisement     * @throws JxtaException if Unable to parse the Advertisement     **/    public static void saveAdvertisementToFile(Advertisement adv, File file) throws JxtaException {        OutputStream out = null;        JxtaException exception = null;                try {            out = new FileOutputStream(file);            Document document = adv.getDocument(MimeMediaType.XML_DEFAULTENCODING);            document.sendToStream(out);        } catch (IOException e) {            exception = new JxtaException("Can't save Advertisement", e);        } finally {            if (out != null) {                try {                    out.close();                } catch (IOException ignored) { ; }            }        }    }        /**     *  Save a JXTA Advertisement as an XML Document to an array of bytes     *     *  @deprecated This method should not be used because it produces a result     *  who's encoding is not predictable and may (will) differ from JVM to JVM.     *     * @param advertisement The Advertisement to be saved     * @throws JxtaException if Unable to parse the Advertisement     **/    public static byte[] advertisementToBytes(Advertisement advertisement) throws JxtaException {        try {            Document document = advertisement.getDocument(MimeMediaType.XML_DEFAULTENCODING);            ByteArrayOutputStream bo = new ByteArrayOutputStream( );            document.sendToStream( bo );                        return bo.toByteArray();        } catch (IOException e) {            throw new JxtaException("Error converting a document to bytes", e);        }    }        /**     *  Convert an array of bytes containing an XML encoded String to an JXTA Advertisement     *     *  @deprecated This method should not be used because it interprets the     *  input using the local default encoding which is not precidcatable and     *  may (will) differ from JVM to JVM.     *     * @param buf The source of the advertisement     * @return The Advertisement     * @throws JxtaException if Unable to parse the Advertisement     **/    public static Advertisement bytesToAdvertisement(byte buf[]) {        try {            InputStream in = new ByteArrayInputStream(buf);            Advertisement advertisement = AdvertisementFactory.newAdvertisement(MimeMediaType.XML_DEFAULTENCODING, in);            return advertisement;        } catch (IOException e) { return null; } // This will never be thrown    }        /**     *  Save a JXTA Advertisement to a String     *     * @param advertisement The Advertisement to be converted     * @param mimeType Type of document to be created     **/    public static String advertisementToText(Advertisement advertisement, String mimeType) {        try {            StructuredTextDocument doc = (StructuredTextDocument) advertisement.getDocument(new MimeMediaType(mimeType));            StringWriter stringWriter = new StringWriter();            doc.sendToWriter(stringWriter);                        return stringWriter.toString();        } catch (IOException e) { return null; } // This will never be thrown    }        /**     *  Save a JXTA Advertisement to a Plain Text String     *     * @param advertisement The Advertisement to be converted     **/    public static String advertisementToPlainText(Advertisement advertisement) {        return advertisementToText(advertisement, MimeMediaType.TEXT_DEFAULTENCODING.toString() );    }        /**     *  Save a JXTA Advertisement to an XML String     *     *  @deprecated Equivalent to Advertisement.toString()     *     * @param advertisement The Advertisement to be converted     **/    public static String advertisementToXmlText(Advertisement advertisement) {        return advertisementToText(advertisement, MimeMediaType.XMLUTF8.toString() );    }                /**     *  Convert an array of bytes containing an XML encoded String to an JXTA Advertisement     *     *  @deprecated This method should not be used because it produces a result     *  who's encoding is not predictable and may (will) differ from JVM to JVM.     *     * @param xmlTextAsBytes buf The source of the advertisement     * @return The Advertisement     * @throws JxtaException if Unable to parse the Advertisement     **/    public static Advertisement newAdvertisementFromXml(byte xmlTextAsBytes[]) throws JxtaException {        try {            return AdvertisementFactory.newAdvertisement(MimeMediaType.XML_DEFAULTENCODING,	new ByteArrayInputStream(xmlTextAsBytes));        } catch(Exception e) {            throw new JxtaException("Unable to create Advertisement from the provided XML", e);        }    }        /**     *  Convert a String containing an XML encoded String to an JXTA Advertisement     *     *  @deprecated This method should not be used because it interprets the     *  input using the local default encoding which is not precidcatable and     *  may (will) differ from JVM to JVM.     *     * @param xmlText The source of the advertisement     * @return The Advertisement     * @throws JxtaException if Unable to parse the Advertisement     **/    public static Advertisement newAdvertisementFromXml(String xmlText) throws JxtaException  {        try {            return AdvertisementFactory.newAdvertisement(MimeMediaType.XML_DEFAULTENCODING,	new StringReader(xmlText) );

⌨️ 快捷键说明

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