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

📄 xmlwriter.java

📁 Web-Harvest是一个Java开源Web数据抽取工具。它能够收集指定的Web页面并从这些页面中提取有用的数据。Web-Harvest主要是运用了像XSLT,XQuery,正则表达式等这些技术来实
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	 * </p>
	 * 
	 * @param uri
	 *            The element's Namespace URI.
	 * @param localName
	 *            The element's local name.
	 * @param qName
	 *            The element's default qualified name.
	 * @param atts
	 *            The element's attributes.
	 * @param content
	 *            The character data content.
	 * @exception org.xml.sax.SAXException
	 *                If there is an error writing the empty tag, or if a
	 *                handler further down the filter chain raises an exception.
	 * @see #startElement(String, String, String, Attributes)
	 * @see #characters(String)
	 * @see #endElement(String, String, String)
	 */
	public void dataElement(String uri, String localName, String qName,
			Attributes atts, String content) throws SAXException {
		startElement(uri, localName, qName, atts);
		characters(content);
		endElement(uri, localName, qName);
	}

	/**
	 * Write an element with character data content but no attributes.
	 * 
	 * <p>
	 * This is a convenience method to write a complete element with character
	 * data content, including the start tag and end tag. This method provides
	 * an empty string for the qname and an empty attribute list.
	 * </p>
	 * 
	 * <p>
	 * This method invokes
	 * {@link #startElement(String, String, String, Attributes)}, followed by
	 * {@link #characters(String)}, followed by
	 * {@link #endElement(String, String, String)}.
	 * </p>
	 * 
	 * @param uri
	 *            The element's Namespace URI.
	 * @param localName
	 *            The element's local name.
	 * @param content
	 *            The character data content.
	 * @exception org.xml.sax.SAXException
	 *                If there is an error writing the empty tag, or if a
	 *                handler further down the filter chain raises an exception.
	 * @see #startElement(String, String, String, Attributes)
	 * @see #characters(String)
	 * @see #endElement(String, String, String)
	 */
	public void dataElement(String uri, String localName, String content)
			throws SAXException {
		dataElement(uri, localName, "", EMPTY_ATTS, content);
	}

	/**
	 * Write an element with character data content but no attributes or
	 * Namespace URI.
	 * 
	 * <p>
	 * This is a convenience method to write a complete element with character
	 * data content, including the start tag and end tag. The method provides an
	 * empty string for the Namespace URI, and empty string for the qualified
	 * name, and an empty attribute list.
	 * </p>
	 * 
	 * <p>
	 * This method invokes
	 * {@link #startElement(String, String, String, Attributes)}, followed by
	 * {@link #characters(String)}, followed by
	 * {@link #endElement(String, String, String)}.
	 * </p>
	 * 
	 * @param localName
	 *            The element's local name.
	 * @param content
	 *            The character data content.
	 * @exception org.xml.sax.SAXException
	 *                If there is an error writing the empty tag, or if a
	 *                handler further down the filter chain raises an exception.
	 * @see #startElement(String, String, String, Attributes)
	 * @see #characters(String)
	 * @see #endElement(String, String, String)
	 */
	public void dataElement(String localName, String content)
			throws SAXException {
		dataElement("", localName, "", EMPTY_ATTS, content);
	}

	/**
	 * Write a string of character data, with XML escaping.
	 * 
	 * <p>
	 * This is a convenience method that takes an XML String, converts it to a
	 * character array, then invokes {@link #characters(char[], int, int)}.
	 * </p>
	 * 
	 * @param data
	 *            The character data.
	 * @exception org.xml.sax.SAXException
	 *                If there is an error writing the string, or if a handler
	 *                further down the filter chain raises an exception.
	 * @see #characters(char[], int, int)
	 */
	public void characters(String data) throws SAXException {
		char ch[] = data.toCharArray();
		characters(ch, 0, ch.length);
	}

	// //////////////////////////////////////////////////////////////////
	// Internal methods.
	// //////////////////////////////////////////////////////////////////

	/**
	 * Force all Namespaces to be declared.
	 * 
	 * This method is used on the root element to ensure that the predeclared
	 * Namespaces all appear.
	 */
	private void forceNSDecls() {
		Enumeration prefixes = forcedDeclTable.keys();
		while (prefixes.hasMoreElements()) {
			String prefix = (String) prefixes.nextElement();
			doPrefix(prefix, null, true);
		}
	}

	/**
	 * Determine the prefix for an element or attribute name.
	 * 
	 * TODO: this method probably needs some cleanup.
	 * 
	 * @param uri
	 *            The Namespace URI.
	 * @param qName
	 *            The qualified name (optional); this will be used to indicate
	 *            the preferred prefix if none is currently bound.
	 * @param isElement
	 *            true if this is an element name, false if it is an attribute
	 *            name (which cannot use the default Namespace).
	 */
	private String doPrefix(String uri, String qName, boolean isElement) {
		String defaultNS = nsSupport.getURI("");
		if ("".equals(uri)) {
			if (isElement && defaultNS != null)
				nsSupport.declarePrefix("", "");
			return null;
		}
		String prefix;
		if (isElement && defaultNS != null && uri.equals(defaultNS)) {
			prefix = "";
		} else {
			prefix = nsSupport.getPrefix(uri);
		}
		if (prefix != null) {
			return prefix;
		}
		prefix = (String) doneDeclTable.get(uri);
		if (prefix != null
				&& ((!isElement || defaultNS != null) && "".equals(prefix) || nsSupport
						.getURI(prefix) != null)) {
			prefix = null;
		}
		if (prefix == null) {
			prefix = (String) prefixTable.get(uri);
			if (prefix != null
					&& ((!isElement || defaultNS != null) && "".equals(prefix) || nsSupport
							.getURI(prefix) != null)) {
				prefix = null;
			}
		}
		if (prefix == null && qName != null && !"".equals(qName)) {
			int i = qName.indexOf(':');
			if (i == -1) {
				if (isElement && defaultNS == null) {
					prefix = "";
				}
			} else {
				prefix = qName.substring(0, i);
			}
		}
		for (; prefix == null || nsSupport.getURI(prefix) != null; prefix = "__NS"
				+ ++prefixCounter)
			;
		nsSupport.declarePrefix(prefix, uri);
		doneDeclTable.put(uri, prefix);
		return prefix;
	}

	/**
	 * Write a raw character.
	 * 
	 * @param c
	 *            The character to write.
	 * @exception org.xml.sax.SAXException
	 *                If there is an error writing the character, this method
	 *                will throw an IOException wrapped in a SAXException.
	 */
	private void write(char c) throws SAXException {
		try {
			output.write(c);
		} catch (IOException e) {
			throw new SAXException(e);
		}
	}

	/**
	 * Write a raw string.
	 * 
	 * @param s
	 * @exception org.xml.sax.SAXException
	 *                If there is an error writing the string, this method will
	 *                throw an IOException wrapped in a SAXException
	 */
	private void write(String s) throws SAXException {
		try {
			output.write(s);
		} catch (IOException e) {
			throw new SAXException(e);
		}
	}

	/**
	 * Write out an attribute list, escaping values.
	 * 
	 * The names will have prefixes added to them.
	 * 
	 * @param atts
	 *            The attribute list to write.
	 * @exception org.xml.SAXException
	 *                If there is an error writing the attribute list, this
	 *                method will throw an IOException wrapped in a
	 *                SAXException.
	 */
	private void writeAttributes(Attributes atts) throws SAXException {
		int len = atts.getLength();
		for (int i = 0; i < len; i++) {
			char ch[] = atts.getValue(i).toCharArray();
			write(' ');
			writeName(atts.getURI(i), atts.getLocalName(i), atts.getQName(i),
					false);
			write("=\"");
			writeEsc(ch, 0, ch.length, true);
			write('"');
		}
	}

	/**
	 * Write an array of data characters with escaping.
	 * 
	 * @param ch
	 *            The array of characters.
	 * @param start
	 *            The starting position.
	 * @param length
	 *            The number of characters to use.
	 * @param isAttVal
	 *            true if this is an attribute value literal.
	 * @exception org.xml.SAXException
	 *                If there is an error writing the characters, this method
	 *                will throw an IOException wrapped in a SAXException.
	 */
	private void writeEsc(char ch[], int start, int length, boolean isAttVal)
			throws SAXException {
		for (int i = start; i < start + length; i++) {
			switch (ch[i]) {
			case '&':
				write("&amp;");
				break;
			case '<':
				write("&lt;");
				break;
			case '>':
				write("&gt;");
				break;
			case '\"':
				if (isAttVal) {
					write("&quot;");
				} else {
					write('\"');
				}
				break;
			default:
				write(ch[i]);
			}
		}
	}

	/**
	 * Write out the list of Namespace declarations.
	 * 
	 * @exception org.xml.sax.SAXException
	 *                This method will throw an IOException wrapped in a
	 *                SAXException if there is an error writing the Namespace
	 *                declarations.
	 */
	private void writeNSDecls() throws SAXException {
		Enumeration prefixes = nsSupport.getDeclaredPrefixes();
		while (prefixes.hasMoreElements()) {
			String prefix = (String) prefixes.nextElement();
			String uri = nsSupport.getURI(prefix);
			if (uri == null) {
				uri = "";
			}
			char ch[] = uri.toCharArray();
			write(' ');
			if ("".equals(prefix)) {
				write("xmlns=\"");
			} else {
				write("xmlns:");
				write(prefix);
				write("=\"");
			}
			writeEsc(ch, 0, ch.length, true);
			write('\"');
		}
	}

	/**
	 * Write an element or attribute name.
	 * 
	 * @param uri
	 *            The Namespace URI.
	 * @param localName
	 *            The local name.
	 * @param qName
	 *            The prefixed name, if available, or the empty string.
	 * @param isElement
	 *            true if this is an element name, false if it is an attribute
	 *            name.
	 * @exception org.xml.sax.SAXException
	 *                This method will throw an IOException wrapped in a
	 *                SAXException if there is an error writing the name.
	 */
	private void writeName(String uri, String localName, String qName,
			boolean isElement) throws SAXException {
		String prefix = doPrefix(uri, qName, isElement);
		if (prefix != null && !"".equals(prefix)) {
			write(prefix);
			write(':');
		}
		if (localName != null && !"".equals(localName)) {
			write(localName);
		} else {
			int i = qName.indexOf(':');
			write(qName.substring(i + 1, qName.length()));
		}
	}

	// //////////////////////////////////////////////////////////////////
	// Default LexicalHandler implementation
	// //////////////////////////////////////////////////////////////////

	public void comment(char[] ch, int start, int length) throws SAXException {
		write("<!--");
		for (int i = start; i < start + length; i++) {
			write(ch[i]);
			if (ch[i] == '-' && i + 1 <= start + length && ch[i + 1] == '-')
				write(' ');
		}
		write("-->");
	}

	public void endCDATA() throws SAXException {
	}

	public void endDTD() throws SAXException {
	}

	public void endEntity(String name) throws SAXException {
	}

	public void startCDATA() throws SAXException {
	}

	public void startDTD(String name, String publicId, String systemId)
			throws SAXException {
	}

	public void startEntity(String name) throws SAXException {
	}

	// //////////////////////////////////////////////////////////////////
	// Output properties
	// //////////////////////////////////////////////////////////////////

	public String getOutputProperty(String key) {
		return outputProperties.getProperty(key);
	}

	public void setOutputProperty(String key, String value) {
		outputProperties.setProperty(key, value);
	}

	// //////////////////////////////////////////////////////////////////
	// Constants.
	// //////////////////////////////////////////////////////////////////

	private final Attributes EMPTY_ATTS = new AttributesImpl();

	public static final String CDATA_SECTION_ELEMENTS = "cdata-section-elements";

	public static final String DOCTYPE_PUBLIC = "doctype-public";

	public static final String DOCTYPE_SYSTEM = "doctype-system";

	public static final String ENCODING = "encoding";

	public static final String INDENT = "indent";

	public static final String MEDIA_TYPE = "media-type";

	public static final String METHOD = "method";

	public static final String OMIT_XML_DECLARATION = "omit-xml-declaration";

	public static final String STANDALONE = "standalone";

	public static final String VERSION = "version";

	// //////////////////////////////////////////////////////////////////
	// Internal state.
	// //////////////////////////////////////////////////////////////////

	private Hashtable prefixTable;

	private Hashtable forcedDeclTable;

	private Hashtable doneDeclTable;

	private int elementLevel = 0;

	private Writer output;

	private NamespaceSupport nsSupport;

	private int prefixCounter = 0;

	private Properties outputProperties;

	private boolean cdataElement = false;

}

// end of XMLWriter.java

⌨️ 快捷键说明

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