PageRenderTime 98ms CodeModel.GetById 84ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/XML/xml/completion/CompletionInfoHandler.java

#
Java | 162 lines | 115 code | 17 blank | 30 comment | 19 complexity | 5387f65e8ae270974eb1d280279aa71f MD5 | raw file
  1/*
  2 * CompletionInfoHandler.java
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2001, 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.completion;
 17
 18//{{{ Imports
 19import java.util.*;
 20import org.gjt.sp.jedit.jEdit;
 21import org.xml.sax.ext.DefaultHandler2;
 22import org.xml.sax.*;
 23import xml.*;
 24//}}}
 25
 26public class CompletionInfoHandler extends DefaultHandler2
 27{
 28	//{{{ CompletionInfoHandler constructor
 29	public CompletionInfoHandler()
 30	{
 31		completionInfo = new CompletionInfo();
 32/*		This is redundant - already done in completionInfo ctor 
 33		addEntity(new EntityDecl(EntityDecl.INTERNAL,"lt","<"));
 34		addEntity(new EntityDecl(EntityDecl.INTERNAL,"gt",">"));
 35		addEntity(new EntityDecl(EntityDecl.INTERNAL,"amp","&"));
 36		addEntity(new EntityDecl(EntityDecl.INTERNAL,"quot","\""));
 37		addEntity(new EntityDecl(EntityDecl.INTERNAL,"apos","'")); */
 38	} //}}}
 39
 40	//{{{ getCompletionInfo() method
 41	public CompletionInfo getCompletionInfo()
 42	{
 43		return completionInfo;
 44	} //}}}
 45
 46	//{{{ setDocumentLocator() method
 47	public void setDocumentLocator(Locator loc)
 48	{
 49		this.loc = loc;
 50	} //}}}
 51
 52	//{{{ resolveEntity() method
 53	public InputSource resolveEntity(String name, String publicId, String baseURI, String systemId)
 54		throws SAXException
 55	{
 56		try
 57		{
 58			return Resolver.instance().resolveEntity(
 59				name,//name
 60				publicId,
 61				/*loc.getSystemId()*/baseURI, //current
 62				systemId);
 63		}
 64		catch(Exception e)
 65		{
 66			throw new SAXException(e);
 67		}
 68	} //}}}
 69
 70	//{{{ startElement() method
 71	public void startElement(String namespaceURI,
 72		String sName, // simple name
 73		String qName, // qualified name
 74		Attributes attrs) throws SAXException
 75	{
 76		if(sName.equals("dtd"))
 77		{
 78			String extend = attrs.getValue("extend");
 79			if(extend != null)
 80			{
 81				String infoURI = jEdit.getProperty(
 82					"mode." + extend
 83					+ ".xml.completion-info");
 84				if(infoURI != null)
 85				{
 86					CompletionInfo extendInfo = CompletionInfo
 87						.getCompletionInfoFromResource(infoURI);
 88					if(extendInfo != null)
 89						completionInfo = (CompletionInfo)extendInfo.clone();
 90				}
 91			}
 92		}
 93		else if(sName.equals("entity"))
 94		{
 95			addEntity(new EntityDecl(
 96				EntityDecl.INTERNAL,
 97				attrs.getValue("name"),
 98				attrs.getValue("value")));
 99		}
100		else if(sName.equals("element"))
101		{
102			element = new ElementDecl(
103				completionInfo,
104				attrs.getValue("name"),
105				attrs.getValue("content"));
106
107			completionInfo.elements.add(element);
108			completionInfo.elementHash.put(element.name,element);
109
110			if("true".equals(attrs.getValue("anywhere")))
111				completionInfo.elementsAllowedAnywhere.add(element);
112		}
113		else if(sName.equals("attribute"))
114		{
115			String name = attrs.getValue("name");
116			String value = attrs.getValue("value");
117			String type = attrs.getValue("type");
118
119			ArrayList values;
120
121			if(type.startsWith("("))
122			{
123				values = new ArrayList();
124
125				StringTokenizer st = new StringTokenizer(
126					type.substring(1,type.length() - 1),"|");
127				while(st.hasMoreTokens())
128				{
129					values.add(st.nextToken());
130				}
131			}
132			else
133				values = null;
134
135			boolean required = "true".equals(attrs.getValue("required"));
136
137			element.addAttribute(new ElementDecl.AttributeDecl(
138				name,null,value,values,type,required));
139		}
140	} //}}}
141
142	//{{{ Private members
143	private CompletionInfo completionInfo;
144	private Locator loc;
145	private ElementDecl element;
146
147	//{{{ addEntity() method
148	private void addEntity(EntityDecl entity)
149	{
150		completionInfo.entities.add(entity);
151		if(entity.type == EntityDecl.INTERNAL
152			&& entity.value.length() == 1)
153		{
154			Character ch = new Character(
155				entity.value.charAt(0));
156			completionInfo.entityHash.put(entity.name,ch);
157			completionInfo.entityHash.put(ch,entity.name);
158		}
159	} //}}}
160
161	//}}}
162}