PageRenderTime 53ms CodeModel.GetById 46ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/XML/xml/parser/XmlTag.java

#
Java | 165 lines | 99 code | 19 blank | 47 comment | 1 complexity | cf4d5ab0741a578dcc2cc407883ca627 MD5 | raw file
  1/*
  2 * XmlTag.java
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2000, 2003 Slava Pestov
  7 *
  8 * The XML plugin is licensed under the GNU General Public License, with
  9 * the following exception:
 10 *
 11 * "Permission is granted to link this code with software released under
 12 * the Apache license version 1.1, for example used by the Xerces XML
 13 * parser package."
 14 */
 15
 16package xml.parser;
 17
 18//{{{ Imports
 19import javax.swing.text.Position;
 20import javax.swing.Icon;
 21import org.gjt.sp.jedit.jEdit;
 22import org.xml.sax.Attributes;
 23import org.xml.sax.helpers.AttributesImpl;
 24import sidekick.Asset;
 25import xml.XmlListCellRenderer;
 26import java.util.Map;
 27import java.util.HashMap;
 28//}}}
 29
 30public class XmlTag extends Asset
 31{
 32	//{{{ Instance variables
 33	public Attributes attributes;
 34	public String attributeString;
 35	public String idAttributeString;
 36	public boolean empty;
 37	public String namespace;
 38	/** namespace -> prefix */
 39	public Map<String,String> namespaceBindings;
 40	private StringBuilder characters = new StringBuilder();
 41	//}}}
 42
 43	//{{{ XmlTag constructor
 44	public XmlTag(String name,String namespace, Position start, Attributes attributes)
 45	{
 46		super(name);
 47		this.namespace = namespace;
 48		this.namespaceBindings = new HashMap<String,String>();
 49		this.start = this.end = start;
 50		this.attributes = new AttributesImpl(attributes);
 51
 52		StringBuffer buf = new StringBuffer();
 53
 54		buf.append(name);
 55
 56		String idName = null;
 57		String idValue = null;
 58
 59		for(int i = 0; i < attributes.getLength(); i++)
 60		{
 61			buf.append(' ');
 62
 63			String aname = attributes.getQName(i);
 64			String value = attributes.getValue(i);
 65			buf.append(aname);
 66			buf.append("=\"");
 67			buf.append(value);
 68			buf.append('"');
 69
 70			if(attributes.getLocalName(i).equalsIgnoreCase("id")
 71				|| attributes.getType(i).equals("ID"))
 72			{
 73				idName = aname;
 74				idValue = value;
 75			}
 76		}
 77
 78		attributeString = buf.toString();
 79
 80		if(idName == null)
 81			idAttributeString = name;
 82		else
 83			idAttributeString = name + ' ' + idName + "=\"" + idValue + '"';
 84	} //}}}
 85
 86	//{{{ getIcon() method
 87	public Icon getIcon()                                                                                        
 88	{
 89		return (empty ? XmlListCellRenderer.EMPTY_ELEMENT_ICON
 90			: XmlListCellRenderer.ELEMENT_ICON);
 91	} //}}}
 92
 93	//{{{ getLongString() method
 94	public String getLongString()
 95	{
 96		return attributeString;
 97	} //}}}
 98
 99	//{{{ getShortString() method
100	public String getShortString()
101	{
102		int showAttributes = jEdit.getIntegerProperty("xml.show-attributes",1);
103		switch(showAttributes)
104		{
105		case 0:
106			return name;
107		case 1:
108			return idAttributeString;
109		case 2:
110			return attributeString;
111		default:
112			return null;
113		}
114	} //}}}
115
116	//{{{ toString() method
117	public String toString()
118	{
119		return attributeString;
120	} //}}}
121	
122	//{{{ getLocalName() method
123	public String getLocalName(){
124		return name.contains(":") ? name.substring(name.indexOf(":")+1) : name;
125	}
126	//}}}
127	
128	//{{{ getPrefix() method
129	public String getPrefix(){
130		return name.contains(":") ? name.substring(0,name.indexOf(":")) : "";
131	}
132	//}}}
133	
134	//{{{ canAddCharacters() method
135	/**
136 	 * @return <code>true</code> if this tag should accumulate the characters from 
137 	 * the body of a tag.  This default implementation returns <code>false</code>.
138 	 * Subclasses may override to return <code>true</code>.
139 	 */
140	public boolean canAddCharacters(){
141		return false;
142	}
143	//}}}
144	
145	
146	//{{{ addCharacters() method
147	/**
148	 * If <code>canAddCharacters</code> returns true, characters from the body of
149 	 * the tag will be accumulated.
150 	 */
151	public void addCharacters(char[] chars){
152		characters.append(chars);
153	}
154	//}}}
155	
156	//{{{ getCharacters() method
157	/**
158 	 * @return Any characters accumulated from the body of the tag.  Will return
159 	 * an empty String if <code>canAddCharacters</code> returns <code>false</code>.
160 	 */
161	public String getCharacters(){
162		return characters.toString();
163	}
164	//}}}
165}