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

📄 xmlwriter.java

📁 Web-Harvest是一个Java开源Web数据抽取工具。它能够收集指定的Web页面并从这些页面中提取有用的数据。Web-Harvest主要是运用了像XSLT,XQuery,正则表达式等这些技术来实
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	/**
	 * Get the current or preferred prefix for a Namespace URI.
	 * 
	 * @param uri
	 *            The Namespace URI.
	 * @return The preferred prefix, or "" for the default Namespace.
	 * @see #setPrefix
	 */
	public String getPrefix(String uri) {
		return (String) prefixTable.get(uri);
	}

	/**
	 * Force a Namespace to be declared on the root element.
	 * 
	 * <p>
	 * By default, the XMLWriter will declare only the Namespaces needed for an
	 * element; as a result, a Namespace may be declared many places in a
	 * document if it is not used on the root element.
	 * </p>
	 * 
	 * <p>
	 * This method forces a Namespace to be declared on the root element even if
	 * it is not used there, and reduces the number of xmlns attributes in the
	 * document.
	 * </p>
	 * 
	 * @param uri
	 *            The Namespace URI to declare.
	 * @see #forceNSDecl(java.lang.String,java.lang.String)
	 * @see #setPrefix
	 */
	public void forceNSDecl(String uri) {
		forcedDeclTable.put(uri, Boolean.TRUE);
	}

	/**
	 * Force a Namespace declaration with a preferred prefix.
	 * 
	 * <p>
	 * This is a convenience method that invokes {@link #setPrefix setPrefix}
	 * then {@link #forceNSDecl(java.lang.String) forceNSDecl}.
	 * </p>
	 * 
	 * @param uri
	 *            The Namespace URI to declare on the root element.
	 * @param prefix
	 *            The preferred prefix for the Namespace, or "" for the default
	 *            Namespace.
	 * @see #setPrefix
	 * @see #forceNSDecl(java.lang.String)
	 */
	public void forceNSDecl(String uri, String prefix) {
		setPrefix(uri, prefix);
		forceNSDecl(uri);
	}

	// //////////////////////////////////////////////////////////////////
	// Methods from org.xml.sax.ContentHandler.
	// //////////////////////////////////////////////////////////////////

	/**
	 * Write the XML declaration at the beginning of the document.
	 * 
	 * Pass the event on down the filter chain for further processing.
	 * 
	 * @exception org.xml.sax.SAXException
	 *                If there is an error writing the XML declaration, or if a
	 *                handler further down the filter chain raises an exception.
	 * @see org.xml.sax.ContentHandler#startDocument
	 */
	public void startDocument() throws SAXException {
		reset();
		if (!("yes".equals(outputProperties.getProperty(OMIT_XML_DECLARATION,
				"no"))))
			write("<?xml version=\"1.0\" standalone=\"yes\"?>\n\n");
		super.startDocument();
	}

	/**
	 * Write a newline at the end of the document.
	 * 
	 * Pass the event on down the filter chain for further processing.
	 * 
	 * @exception org.xml.sax.SAXException
	 *                If there is an error writing the newline, or if a handler
	 *                further down the filter chain raises an exception.
	 * @see org.xml.sax.ContentHandler#endDocument
	 */
	public void endDocument() throws SAXException {
		write('\n');
		super.endDocument();
		try {
			flush();
		} catch (IOException e) {
			throw new SAXException(e);
		}
	}

	/**
	 * Write a start tag.
	 * 
	 * Pass the event on down the filter chain for further processing.
	 * 
	 * @param uri
	 *            The Namespace URI, or the empty string if none is available.
	 * @param localName
	 *            The element's local (unprefixed) name (required).
	 * @param qName
	 *            The element's qualified (prefixed) name, or the empty string
	 *            is none is available. This method will use the qName as a
	 *            template for generating a prefix if necessary, but it is not
	 *            guaranteed to use the same qName.
	 * @param atts
	 *            The element's attribute list (must not be null).
	 * @exception org.xml.sax.SAXException
	 *                If there is an error writing the start tag, or if a
	 *                handler further down the filter chain raises an exception.
	 * @see org.xml.sax.ContentHandler#startElement
	 */
	public void startElement(String uri, String localName, String qName,
			Attributes atts) throws SAXException {
		elementLevel++;
		nsSupport.pushContext();
		write('<');
		writeName(uri, localName, qName, true);
		writeAttributes(atts);
		if (elementLevel == 1) {
			forceNSDecls();
		}
		writeNSDecls();
		write('>');
		if ("html".equals(outputProperties.getProperty(METHOD, "xml"))
				&& (localName.equals("script") || localName.equals("style"))) {
			cdataElement = true;
		}
		super.startElement(uri, localName, qName, atts);
	}

	/**
	 * Write an end tag.
	 * 
	 * Pass the event on down the filter chain for further processing.
	 * 
	 * @param uri
	 *            The Namespace URI, or the empty string if none is available.
	 * @param localName
	 *            The element's local (unprefixed) name (required).
	 * @param qName
	 *            The element's qualified (prefixed) name, or the empty string
	 *            is none is available. This method will use the qName as a
	 *            template for generating a prefix if necessary, but it is not
	 *            guaranteed to use the same qName.
	 * @exception org.xml.sax.SAXException
	 *                If there is an error writing the end tag, or if a handler
	 *                further down the filter chain raises an exception.
	 * @see org.xml.sax.ContentHandler#endElement
	 */
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if (!("html".equals(outputProperties.getProperty(METHOD, "xml"))
				&& uri.equals("http://www.w3.org/1999/xhtml") && (localName
				.equals("area")
				|| localName.equals("base")
				|| localName.equals("basefont")
				|| localName.equals("br")
				|| localName.equals("col")
				|| localName.equals("frame")
				|| localName.equals("hr")
				|| localName.equals("img")
				|| localName.equals("input")
				|| localName.equals("isindex")
				|| localName.equals("link")
				|| localName.equals("meta") || localName.equals("param")))) {
			write("</");
			writeName(uri, localName, qName, true);
			write('>');
		}
		if (elementLevel == 1) {
			write('\n');
		}
		cdataElement = false;
		super.endElement(uri, localName, qName);
		nsSupport.popContext();
		elementLevel--;
	}

	/**
	 * Write character data.
	 * 
	 * Pass the event on down the filter chain for further processing.
	 * 
	 * @param ch
	 *            The array of characters to write.
	 * @param start
	 *            The starting position in the array.
	 * @param length
	 *            The number of characters to write.
	 * @exception org.xml.sax.SAXException
	 *                If there is an error writing the characters, or if a
	 *                handler further down the filter chain raises an exception.
	 * @see org.xml.sax.ContentHandler#characters
	 */
	public void characters(char ch[], int start, int len) throws SAXException {
		if (!cdataElement) {
			writeEsc(ch, start, len, false);
		} else {
			for (int i = start; i < start + len; i++) {
				write(ch[i]);
			}
		}
		super.characters(ch, start, len);
	}

	/**
	 * Write ignorable whitespace.
	 * 
	 * Pass the event on down the filter chain for further processing.
	 * 
	 * @param ch
	 *            The array of characters to write.
	 * @param start
	 *            The starting position in the array.
	 * @param length
	 *            The number of characters to write.
	 * @exception org.xml.sax.SAXException
	 *                If there is an error writing the whitespace, or if a
	 *                handler further down the filter chain raises an exception.
	 * @see org.xml.sax.ContentHandler#ignorableWhitespace
	 */
	public void ignorableWhitespace(char ch[], int start, int length)
			throws SAXException {
		writeEsc(ch, start, length, false);
		super.ignorableWhitespace(ch, start, length);
	}

	/**
	 * Write a processing instruction.
	 * 
	 * Pass the event on down the filter chain for further processing.
	 * 
	 * @param target
	 *            The PI target.
	 * @param data
	 *            The PI data.
	 * @exception org.xml.sax.SAXException
	 *                If there is an error writing the PI, or if a handler
	 *                further down the filter chain raises an exception.
	 * @see org.xml.sax.ContentHandler#processingInstruction
	 */
	public void processingInstruction(String target, String data)
			throws SAXException {
		write("<?");
		write(target);
		write(' ');
		write(data);
		write("?>");
		if (elementLevel < 1) {
			write('\n');
		}
		super.processingInstruction(target, data);
	}

	// //////////////////////////////////////////////////////////////////
	// Additional markup.
	// //////////////////////////////////////////////////////////////////

	/**
	 * Write an empty element.
	 * 
	 * This method writes an empty element tag rather than a start tag followed
	 * by an end tag. Both a {@link #startElement startElement} and an
	 * {@link #endElement endElement} event will be passed on down the filter
	 * chain.
	 * 
	 * @param uri
	 *            The element's Namespace URI, or the empty string if the
	 *            element has no Namespace or if Namespace processing is not
	 *            being performed.
	 * @param localName
	 *            The element's local name (without prefix). This parameter must
	 *            be provided.
	 * @param qName
	 *            The element's qualified name (with prefix), or the empty
	 *            string if none is available. This parameter is strictly
	 *            advisory: the writer may or may not use the prefix attached.
	 * @param atts
	 *            The element's attribute list.
	 * @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
	 * @see #endElement
	 */
	public void emptyElement(String uri, String localName, String qName,
			Attributes atts) throws SAXException {
		nsSupport.pushContext();
		write('<');
		writeName(uri, localName, qName, true);
		writeAttributes(atts);
		if (elementLevel == 1) {
			forceNSDecls();
		}
		writeNSDecls();
		write("/>");
		super.startElement(uri, localName, qName, atts);
		super.endElement(uri, localName, qName);
	}

	// //////////////////////////////////////////////////////////////////
	// Convenience methods.
	// //////////////////////////////////////////////////////////////////

	/**
	 * Start a new element without a qname or attributes.
	 * 
	 * <p>
	 * This method will provide a default empty attribute list and an empty
	 * string for the qualified name. It invokes {@link  #startElement(String,
	 * String, String, Attributes)} directly.
	 * </p>
	 * 
	 * @param uri
	 *            The element's Namespace URI.
	 * @param localName
	 *            The element's local name.
	 * @exception org.xml.sax.SAXException
	 *                If there is an error writing the start tag, or if a
	 *                handler further down the filter chain raises an exception.
	 * @see #startElement(String, String, String, Attributes)
	 */
	public void startElement(String uri, String localName) throws SAXException {
		startElement(uri, localName, "", EMPTY_ATTS);
	}

	/**
	 * Start a new element without a qname, attributes or a Namespace URI.
	 * 
	 * <p>
	 * This method will provide an empty string for the Namespace URI, and empty
	 * string for the qualified name, and a default empty attribute list. It
	 * invokes #startElement(String, String, String, Attributes)} directly.
	 * </p>
	 * 
	 * @param localName
	 *            The element's local name.
	 * @exception org.xml.sax.SAXException
	 *                If there is an error writing the start tag, or if a
	 *                handler further down the filter chain raises an exception.
	 * @see #startElement(String, String, String, Attributes)
	 */
	public void startElement(String localName) throws SAXException {
		startElement("", localName, "", EMPTY_ATTS);
	}

	/**
	 * End an element without a qname.
	 * 
	 * <p>
	 * This method will supply an empty string for the qName. It invokes
	 * {@link #endElement(String, String, String)} directly.
	 * </p>
	 * 
	 * @param uri
	 *            The element's Namespace URI.
	 * @param localName
	 *            The element's local name.
	 * @exception org.xml.sax.SAXException
	 *                If there is an error writing the end tag, or if a handler
	 *                further down the filter chain raises an exception.
	 * @see #endElement(String, String, String)
	 */
	public void endElement(String uri, String localName) throws SAXException {
		endElement(uri, localName, "");
	}

	/**
	 * End an element without a Namespace URI or qname.
	 * 
	 * <p>
	 * This method will supply an empty string for the qName and an empty string
	 * for the Namespace URI. It invokes
	 * {@link #endElement(String, String, String)} directly.
	 * </p>
	 * 
	 * @param localName
	 *            The element's local name.
	 * @exception org.xml.sax.SAXException
	 *                If there is an error writing the end tag, or if a handler
	 *                further down the filter chain raises an exception.
	 * @see #endElement(String, String, String)
	 */
	public void endElement(String localName) throws SAXException {
		endElement("", localName, "");
	}

	/**
	 * Add an empty element without a qname or attributes.
	 * 
	 * <p>
	 * This method will supply an empty string for the qname and an empty
	 * attribute list. It invokes
	 * {@link #emptyElement(String, String, String, Attributes)} directly.
	 * </p>
	 * 
	 * @param uri
	 *            The element's Namespace URI.
	 * @param localName
	 *            The element's local name.
	 * @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 #emptyElement(String, String, String, Attributes)
	 */
	public void emptyElement(String uri, String localName) throws SAXException {
		emptyElement(uri, localName, "", EMPTY_ATTS);
	}

	/**
	 * Add an empty element without a Namespace URI, qname or attributes.
	 * 
	 * <p>
	 * This method will supply an empty string for the qname, and empty string
	 * for the Namespace URI, and an empty attribute list. It invokes
	 * {@link #emptyElement(String, String, String, Attributes)} directly.
	 * </p>
	 * 
	 * @param localName
	 *            The element's local name.
	 * @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 #emptyElement(String, String, String, Attributes)
	 */
	public void emptyElement(String localName) throws SAXException {
		emptyElement("", localName, "", EMPTY_ATTS);
	}

	/**
	 * Write an element with character data content.
	 * 
	 * <p>
	 * This is a convenience method to write a complete element with character
	 * data content, including the start tag and end tag.
	 * </p>
	 * 
	 * <p>
	 * This method invokes
	 * {@link #startElement(String, String, String, Attributes)}, followed by
	 * {@link #characters(String)}, followed by
	 * {@link #endElement(String, String, String)}.

⌨️ 快捷键说明

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