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

📄 xmlwriter.java

📁 pastry的java实现的2.0b版
💻 JAVA
字号:
/*************************************************************************"FreePastry" Peer-to-Peer Application Development Substrate Copyright 2002, Rice University. All rights reserved.Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions aremet:- Redistributions of source code must retain the above copyrightnotice, this list of conditions and the following disclaimer.- Redistributions in binary form must reproduce the above copyrightnotice, this list of conditions and the following disclaimer in thedocumentation and/or other materials provided with the distribution.- Neither  the name  of Rice  University (RICE) nor  the names  of itscontributors may be  used to endorse or promote  products derived fromthis software without specific prior written permission.This software is provided by RICE and the contributors on an "as is"basis, without any representations or warranties of any kind, expressor implied including, but not limited to, representations orwarranties of non-infringement, merchantability or fitness for aparticular purpose. In no event shall RICE or contributors be liablefor any direct, indirect, incidental, special, exemplary, orconsequential damages (including, but not limited to, procurement ofsubstitute goods or services; loss of use, data, or profits; orbusiness interruption) however caused and on any theory of liability,whether in contract, strict liability, or tort (including negligenceor otherwise) arising in any way out of the use of this software, evenif advised of the possibility of such damage.********************************************************************************/package rice.p2p.util;import java.io.*;import org.xmlpull.v1.*;/** * XMLWriter is a utility class used by XMLObjectOutputStream to perform the * actual XML writing. This writing is based on the XML Pull-Parsing API, * available online at http://www.xmlpull.org. Any of the provided serializer * implementations will work with this reader. * * @version $Id: XMLWriter.java 2324 2005-03-14 23:00:27Z amislove $ * @author Alan Mislove */public class XMLWriter {  /**   * The actual XML serializer, which does the writing   */  protected XmlSerializer serializer;  /**   * The underlying writer which the serializer uses   */  protected Writer writer;  /**   * Constructor which takes the provided writer and builds a new XML writier to   * read XML from the writier.   *   * @param out The writer to base this XML writer off of   * @exception IOException DESCRIBE THE EXCEPTION   * @throws IOException If an error occurs   */  public XMLWriter(OutputStream out) throws IOException {    try {      this.writer = new BufferedWriter(new OutputStreamWriter(out));      XmlPullParserFactory factory = XmlPullParserFactory.newInstance(System.getProperty(XmlPullParserFactory.PROPERTY_NAME), null);      serializer = factory.newSerializer();      serializer.setOutput(this.writer);      serializer.setProperty("http://xmlpull.org/v1/doc/properties.html#serializer-indentation", " ");      serializer.setProperty("http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", "\n");      serializer.setFeature("http://xmlpull.org/v1/doc/features.html#serializer-attvalue-use-apostrophe", true);    } catch (XmlPullParserException e) {      throw new IOException("XML Exception thrown: " + e);    }  }  /**   * Method which flushes all buffered data to the underlying writer   *   * @throws IOException If an error occurs   */  public void flush() throws IOException {    serializer.flush();  }  /**   * Method which flushes and closes the underlying writer, which will cause   * future writer attempts to throw an IOException.   *   * @throws IOException If an error occurs   */  public void close() throws IOException {    serializer.text("\n");    serializer.flush();    writer.close();  }  /**   * Method which writes a sequence of base64 encoded bytes to the output stream   *   * @param bytes The bytes to write   * @param off DESCRIBE THE PARAMETER   * @param len DESCRIBE THE PARAMETER   * @exception IOException DESCRIBE THE EXCEPTION   */  public void writeBase64(byte[] bytes, int off, int len) throws IOException {    flush();    writer.write(Base64.encodeBytes(bytes, off, len));  }  /**   * Method which writes the XML header to the writer.   *   * @throws IOException If an error occurs   */  public void writeHeader() throws IOException {    serializer.startDocument(null, null);    serializer.text("\n\n");  }  /**   * Method which writes an attribute to the XML document.   *   * @param name The name of the attribute to write   * @param value The value to write   * @throws IOException If an error occurs   */  public void attribute(String name, int value) throws IOException {    attribute(name, String.valueOf(value));  }  /**   * Method which writes an attribute to the XML document.   *   * @param name The name of the attribute to write   * @param value The value to write   * @throws IOException If an error occurs   */  public void attribute(String name, double value) throws IOException {    attribute(name, String.valueOf(value));  }  /**   * Method which writes an attribute to the XML document.   *   * @param name The name of the attribute to write   * @param value The value to write   * @throws IOException If an error occurs   */  public void attribute(String name, float value) throws IOException {    attribute(name, String.valueOf(value));  }  /**   * Method which writes an attribute to the XML document.   *   * @param name The name of the attribute to write   * @param value The value to write   * @throws IOException If an error occurs   */  public void attribute(String name, long value) throws IOException {    attribute(name, String.valueOf(value));  }  /**   * Method which writes an attribute to the XML document.   *   * @param name The name of the attribute to write   * @param value The value to write   * @throws IOException If an error occurs   */  public void attribute(String name, char value) throws IOException {    attribute(name, String.valueOf(value));  }  /**   * Method which writes an attribute to the XML document.   *   * @param name The name of the attribute to write   * @param value The value to write   * @throws IOException If an error occurs   */  public void attribute(String name, boolean value) throws IOException {    attribute(name, String.valueOf(value));  }  /**   * Method which writes an attribute to the XML document.   *   * @param name The name of the attribute to write   * @param value The value to write   * @throws IOException If an error occurs   */  public void attribute(String name, Object value) throws IOException {    if (value == null) {      return;    }    attribute(name, value.toString());  }  /**   * Method which writes an attribute to the XML document.   *   * @param name The name of the attribute to write   * @param value The value to write   * @throws IOException If an error occurs   */  protected void attribute(String name, String value) throws IOException {    serializer.attribute(null, name, value);  }  /**   * Method which starts the given tag name   *   * @param name The name of the tag to start   * @throws IOException If an error occurs   */  public void start(String name) throws IOException {    serializer.startTag(null, name);  }  /**   * Method which ends the given tag name   *   * @param name The name of the tag to end   * @throws IOException If an error occurs   */  public void end(String name) throws IOException {    serializer.endTag(null, name);  }}

⌨️ 快捷键说明

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