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

📄 htmlwriter.java

📁 Mobile 应用程序使用 Java Micro Edition (Java ME) 平台
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                write("<base href=\"" + document.getBase() + "\">");                writeLineSeparator();            }            decrIndent();	}	    }        /**     * Writes out text that is contained in a TEXTAREA form     * element.     *     * @param attr  an AttributeSet     * @exception IOException on any I/O error     * @exception BadLocationException if pos represents an invalid     *            location within the document.     */    protected void textAreaContent(AttributeSet attr) throws BadLocationException, IOException {	Document doc = (Document)attr.getAttribute(StyleConstants.ModelAttribute);	if (doc != null && doc.getLength() > 0) {	    if (segment == null) {		segment = new Segment();	    }	    doc.getText(0, doc.getLength(), segment);	    if (segment.count > 0) {		inTextArea = true;		incrIndent();		indentSmart();		setCanWrapLines(true);		replaceEntities = true;		write(segment.array, segment.offset, segment.count);		replaceEntities = false;		setCanWrapLines(false);		writeLineSeparator();		inTextArea = false;		decrIndent();	    }	}    }    /**     * Writes out text.  If a range is specified when the constructor     * is invoked, then only the appropriate range of text is written     * out.     *     * @param elem   an Element     * @exception IOException on any I/O error     * @exception BadLocationException if pos represents an invalid     *            location within the document.     */    protected void text(Element elem) throws BadLocationException, IOException {	int start = Math.max(getStartOffset(), elem.getStartOffset());	int end = Math.min(getEndOffset(), elem.getEndOffset());	if (start < end) {	    if (segment == null) {		segment = new Segment();	    }	    getDocument().getText(start, end - start, segment);	    newlineOutputed = false;	    if (segment.count > 0) {		if (segment.array[segment.offset + segment.count - 1] == '\n'){		    newlineOutputed = true;		}		if (inPre && end == preEndOffset) {		    if (segment.count > 1) {			segment.count--;		    }		    else {			return;		    }		}		replaceEntities = true;		setCanWrapLines(!inPre);		write(segment.array, segment.offset, segment.count);		setCanWrapLines(false);		replaceEntities = false;	    }	}    }    /**     * Writes out the content of the SELECT form element.     *      * @param attr the AttributeSet associated with the form element     * @exception IOException on any I/O error     */    protected void selectContent(AttributeSet attr) throws IOException {	Object model = attr.getAttribute(StyleConstants.ModelAttribute);	incrIndent();	if (model instanceof OptionListModel) {	    OptionListModel listModel = (OptionListModel)model;	    int size = listModel.getSize();	    for (int i = 0; i < size; i++) {		Option option = (Option)listModel.getElementAt(i);		writeOption(option);	    }	} else if (model instanceof OptionComboBoxModel) {	    OptionComboBoxModel comboBoxModel = (OptionComboBoxModel)model;	    int size = comboBoxModel.getSize();	    for (int i = 0; i < size; i++) {		Option option = (Option)comboBoxModel.getElementAt(i);		writeOption(option);	    }	}	decrIndent();    }    /**     * Writes out the content of the Option form element.     * @param option  an Option     * @exception IOException on any I/O error     *      */    protected void writeOption(Option option) throws IOException {		indentSmart();	write('<');	write("option");        // PENDING: should this be changed to check for null first?        Object value = option.getAttributes().getAttribute                              (HTML.Attribute.VALUE);	if (value != null) {	    write(" value="+ value);	}	if (option.isSelected()) {	    write(" selected");	}	write('>');	if (option.getLabel() != null) {	    write(option.getLabel());	}	writeLineSeparator();    }    /**     * Writes out an end tag for the element.     *     * @param elem    an Element     * @exception IOException on any I/O error     */    protected void endTag(Element elem) throws IOException {	if (synthesizedElement(elem)) {	    return;	}	// write out end tags for item on stack	closeOutUnwantedEmbeddedTags(elem.getAttributes());	if (inContent) { 	    if (!newlineOutputed && !inPre) {		writeLineSeparator();	    }	    newlineOutputed = false;	    inContent = false;	}	if (!inPre) {	    indentSmart();	}	if (matchNameAttribute(elem.getAttributes(), HTML.Tag.PRE)) {	    inPre = false;	}        write('<');        write('/');        write(elem.getName());        write('>');	writeLineSeparator();    }    /**     * Writes out comments.     *     * @param elem    an Element     * @exception IOException on any I/O error     * @exception BadLocationException if pos represents an invalid     *            location within the document.     */    protected void comment(Element elem) throws BadLocationException, IOException {	AttributeSet as = elem.getAttributes();	if (matchNameAttribute(as, HTML.Tag.COMMENT)) {	    Object comment = as.getAttribute(HTML.Attribute.COMMENT);	    if (comment instanceof String) {		writeComment((String)comment);	    }	    else {		writeComment(null);	    }	}    }    /**     * Writes out comment string.     *     * @param string   the comment     * @exception IOException on any I/O error     * @exception BadLocationException if pos represents an invalid     *            location within the document.     */    void writeComment(String string) throws IOException {	write("<!--");	if (string != null) {	    write(string);	}	write("-->");	writeLineSeparator();        indentSmart();    }    /**     * Writes out any additional comments (comments outside of the body)     * stored under the property HTMLDocument.AdditionalComments.     */    void writeAdditionalComments() throws IOException {	Object comments = getDocument().getProperty	                                (HTMLDocument.AdditionalComments);	if (comments instanceof Vector) {	    Vector v = (Vector)comments;	    for (int counter = 0, maxCounter = v.size(); counter < maxCounter;		 counter++) {		writeComment(v.elementAt(counter).toString());	    }	}    }    /**     * Returns true if the element is a     * synthesized element.  Currently we are only testing     * for the p-implied tag.     */    protected boolean synthesizedElement(Element elem) {	if (matchNameAttribute(elem.getAttributes(), HTML.Tag.IMPLIED)) {	    return true;	}	return false;    }    /**     * Returns true if the StyleConstants.NameAttribute is     * equal to the tag that is passed in as a parameter.     */    protected boolean matchNameAttribute(AttributeSet attr, HTML.Tag tag) {	Object o = attr.getAttribute(StyleConstants.NameAttribute);	if (o instanceof HTML.Tag) {	    HTML.Tag name = (HTML.Tag) o;	    if (name == tag) {		return true;	    }	}	return false;    }    /**     * Searches for embedded tags in the AttributeSet     * and writes them out.  It also stores these tags in a vector     * so that when appropriate the corresponding end tags can be     * written out.     *     * @exception IOException on any I/O error     */    protected void writeEmbeddedTags(AttributeSet attr) throws IOException {		// translate css attributes to html	attr = convertToHTML(attr, oConvAttr);	Enumeration names = attr.getAttributeNames();	while (names.hasMoreElements()) {	    Object name = names.nextElement();	    if (name instanceof HTML.Tag) {		HTML.Tag tag = (HTML.Tag)name;		if (tag == HTML.Tag.FORM || tags.contains(tag)) {		    continue;		}		write('<');		write(tag.toString());		Object o = attr.getAttribute(tag);		if (o != null && o instanceof AttributeSet) {		    writeAttributes((AttributeSet)o);		}		write('>');		tags.addElement(tag);		tagValues.addElement(o);	    }	}    }    /**     * Searches the attribute set for a tag, both of which     * are passed in as a parameter.  Returns true if no match is found     * and false otherwise.     */    private boolean noMatchForTagInAttributes(AttributeSet attr, HTML.Tag t,					      Object tagValue) {	if (attr != null && attr.isDefined(t)) {	    Object newValue = attr.getAttribute(t);	    if ((tagValue == null) ? (newValue == null) :		(newValue != null && tagValue.equals(newValue))) {		return false;	    }	}	return true;    }    /**     * Searches the attribute set and for each tag     * that is stored in the tag vector.  If the tag isnt found,     * then the tag is removed from the vector and a corresponding     * end tag is written out.     *     * @exception IOException on any I/O error     */    protected void closeOutUnwantedEmbeddedTags(AttributeSet attr) throws IOException {	tagsToRemove.removeAllElements();	// translate css attributes to html	attr = convertToHTML(attr, null);	HTML.Tag t;	Object tValue;	int firstIndex = -1;	int size = tags.size();	// First, find all the tags that need to be removed.	for (int i = size - 1; i >= 0; i--) {	    t = (HTML.Tag)tags.elementAt(i);	    tValue = tagValues.elementAt(i);	    if ((attr == null) || noMatchForTagInAttributes(attr, t, tValue)) {		firstIndex = i;		tagsToRemove.addElement(t);	    }	}	if (firstIndex != -1) {	    // Then close them out.	    boolean removeAll = ((size - firstIndex) == tagsToRemove.size());	    for (int i = size - 1; i >= firstIndex; i--) {		t = (HTML.Tag)tags.elementAt(i);		if (removeAll || tagsToRemove.contains(t)) {		    tags.removeElementAt(i);		    tagValues.removeElementAt(i);		}		write('<');		write('/');		write(t.toString());		write('>');	    }	    // Have to output any tags after firstIndex that still remaing,	    // as we closed them out, but they should remain open.	    size = tags.size();	    for (int i = firstIndex; i < size; i++) {		t = (HTML.Tag)tags.elementAt(i);		write('<');		write(t.toString());		Object o = tagValues.elementAt(i);		if (o != null && o instanceof AttributeSet) {		    writeAttributes((AttributeSet)o);		}		write('>');	    }	}    }    /**     * Determines if the element associated with the attributeset     * is a TEXTAREA or SELECT.  If true, returns true else     * false     */    private boolean isFormElementWithContent(AttributeSet attr) {	if (matchNameAttribute(attr, HTML.Tag.TEXTAREA) ||	    matchNameAttribute(attr, HTML.Tag.SELECT)) {	    return true;	}	return false;    }    /**     * Determines whether a the indentation needs to be     * incremented.  Basically, if next is a child of current, and     * next is NOT a synthesized element, the indent level will be     * incremented.  If there is a parent-child relationship and "next"     * is a synthesized element, then its children must be indented.     * This state is maintained by the indentNext boolean.     *     * @return boolean that's true if indent level     *         needs incrementing.     */    private boolean indentNext = false;    private boolean indentNeedsIncrementing(Element current, Element next) {	if ((next.getParentElement() == current) && !inPre) {	    if (indentNext) {		indentNext = false;		return true;	    } else if (synthesizedElement(next)) {		indentNext = true;	    } else if (!synthesizedElement(current)){		return true;	    }	}	return false;    }    /**     * Outputs the maps as elements. Maps are not stored as elements in     * the document, and as such this is used to output them.     */    void writeMaps(Enumeration maps) throws IOException {	if (maps != null) {	    while(maps.hasMoreElements()) {		Map map = (Map)maps.nextElement();

⌨️ 快捷键说明

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