blob: 4068f52615d7daa20f1554316e2edfeef97b0077 [file] [log] [blame]
nitind958d79a2004-11-23 19:23:00 +00001/*****************************************************************************
2 * Copyright (c) 2004 IBM Corporation and others. All rights reserved. This
3 * program and the accompanying materials are made available under the terms
4 * of the Eclipse Public License v1.0 which accompanies this distribution, and
5 * is available at http://www.eclipse.org/legal/epl-v10.html
6 *
7 * Contributors: IBM Corporation - initial API and implementation
8 ****************************************************************************/
david_williamsaaadf2b2005-04-11 07:28:31 +00009package org.eclipse.wst.css.ui.internal.contentassist;
nitind958d79a2004-11-23 19:23:00 +000010
11
12
13import java.util.ArrayList;
14import java.util.Collections;
15import java.util.Iterator;
16import java.util.List;
17import java.util.Vector;
18
david_williams63219a22005-04-10 01:59:51 +000019import org.eclipse.wst.css.core.internal.provisional.document.ICSSModel;
20import org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule;
david_williamsaaadf2b2005-04-11 07:28:31 +000021import org.eclipse.wst.css.ui.internal.image.CSSImageType;
david_williams56777022005-04-11 06:21:55 +000022import org.eclipse.wst.html.core.internal.contentmodel.HTMLCMDocumentFactory;
david_williamsc06c86f2005-03-18 18:23:41 +000023import org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration;
24import org.eclipse.wst.xml.core.internal.contentmodel.CMDocument;
25import org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration;
26import org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap;
david_williams646a5e22005-04-02 07:16:27 +000027import org.eclipse.wst.xml.core.internal.document.DocumentTypeAdapter;
david_williams56777022005-04-11 06:21:55 +000028import org.eclipse.wst.xml.core.internal.provisional.contentmodel.CMDocType;
david_williams4ad020f2005-04-18 08:00:30 +000029import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument;
nitind958d79a2004-11-23 19:23:00 +000030import org.w3c.dom.Document;
31import org.w3c.dom.Node;
32
33class CSSProposalGeneratorForHTMLTag extends CSSProposalGenerator {
34
35 /**
36 * CSSStyleRuleProposalGenerator constructor comment.
37 *
nitind958d79a2004-11-23 19:23:00 +000038 */
39 CSSProposalGeneratorForHTMLTag(CSSContentAssistContext context) {
40 super(context);
41
42 if (fHTMLTags == null) {
43 fHTMLTags = setupHTMLTags();
44 }
45 }
46
47 /**
48 * getCandidates method comment.
49 */
50 protected Iterator getCandidates() {
51 List candidates = new ArrayList();
52
53 if (checkLeadingColon()) {
54 return candidates.iterator();
55 }
56
57 // XHTML requires lower case
58 boolean bLowerCase = false;
59 if (fContext.getModel().getStyleSheetType() == ICSSModel.EMBEDDED) {
60 Node domNode = fContext.getModel().getOwnerDOMNode();
61 if (domNode != null && !(domNode instanceof Document)) {
62 domNode = domNode.getOwnerDocument();
david_williamsc39caaf2005-04-05 06:07:16 +000063 if (domNode instanceof IDOMDocument) {
64 DocumentTypeAdapter adapter = (DocumentTypeAdapter) ((IDOMDocument) domNode).getAdapterFor(DocumentTypeAdapter.class);
nitind958d79a2004-11-23 19:23:00 +000065 if (adapter != null)
66 bLowerCase = (adapter.getTagNameCase() == DocumentTypeAdapter.LOWER_CASE);
67 }
68 }
69 }
70
71
72 int length = fHTMLTags.length;
73 for (int i = 0; i < length; i++) {
74 String tagText = fHTMLTags[i];
75 if (bLowerCase) {
76 tagText = tagText.toLowerCase();
77 }
78 if (!isMatch(tagText)) {
79 continue;
80 }
81
82 int cursorPos = 0;
83 StringBuffer buf = new StringBuffer();
84 buf.append(tagText);
85 cursorPos += tagText.length();
86 boolean inRule = (fContext.getTargetNode() instanceof ICSSStyleRule);
87 if (!inRule || fContext.getTextToReplace().length() == 0) {
88 buf.append(" ");//$NON-NLS-1$
89 cursorPos += 1;
90 }
91 if (!inRule) {
92 StringAndOffset sao = generateBraces();
93 buf.append(sao.fString);
94 cursorPos += sao.fOffset;
95 }
96
97 CSSCACandidate item = new CSSCACandidate();
98 item.setReplacementString(buf.toString());
99 item.setCursorPosition(cursorPos);
100 item.setDisplayString(tagText);
101 item.setImageType(CSSImageType.SELECTOR_TAG);
102 candidates.add(item);
103 }
104
105 return candidates.iterator();
106 }
107
108 /**
109 *
110 */
111 private static String[] setupHTMLTags() {
112 CMDocument cmdoc = HTMLCMDocumentFactory.getCMDocument(CMDocType.HTML_DOC_TYPE);
113 CMNamedNodeMap elements = cmdoc.getElements();
114 Vector names = new Vector();
115 int nElements = elements.getLength();
116 for (int i = 0; i < nElements; i++) {
117 CMElementDeclaration edec = (CMElementDeclaration) elements.item(i);
118 if (isAttrDefined(edec, HTML40Namespace.ATTR_NAME_STYLE)) {
119 names.add(edec.getElementName());
120 }
121 }
122 Collections.sort(names);
123 String[] tags = new String[names.size()];
124 Iterator iNames = names.iterator();
125 for (int i = 0; iNames.hasNext(); i++) {
126 tags[i] = (String) iNames.next();
127 }
128 return tags;
129 }
130
131 /**
132 *
133 */
134 private static boolean isAttrDefined(CMElementDeclaration edec, String attrName) {
135 if (edec == null) {
136 return false;
137 }
138 CMNamedNodeMap attrs = edec.getAttributes();
139 if (attrs == null) {
140 return false;
141 }
142 for (int i = 0; i < attrs.getLength(); i++) {
143 CMAttributeDeclaration attr = (CMAttributeDeclaration) attrs.item(i);
144 if (attr == null) {
145 continue;
146 }
147 if (attr.getAttrName().equalsIgnoreCase(attrName)) {
148 return true;
149 }
150 }
151 return false;
152 }
153
154 private static String[] fHTMLTags = null;
155}