blob: ee81dd5fd2dee6b8b212e49a5f32388b29bf2e00 [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 ****************************************************************************/
9package org.eclipse.wst.css.ui.contentassist;
10
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;
nitind958d79a2004-11-23 19:23:00 +000021import org.eclipse.wst.css.ui.image.CSSImageType;
22import org.eclipse.wst.html.core.contentmodel.HTMLCMDocumentFactory;
david_williamsc06c86f2005-03-18 18:23:41 +000023import org.eclipse.wst.xml.core.contentmodel.CMDocType;
david_williamsc39caaf2005-04-05 06:07:16 +000024import org.eclipse.wst.xml.core.document.IDOMDocument;
david_williamsc06c86f2005-03-18 18:23:41 +000025import org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration;
26import org.eclipse.wst.xml.core.internal.contentmodel.CMDocument;
27import org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration;
28import org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap;
david_williams646a5e22005-04-02 07:16:27 +000029import org.eclipse.wst.xml.core.internal.document.DocumentTypeAdapter;
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 *
38 * @param context
39 * com.ibm.sed.contentassist.old.css.CSSContentAssistContext
40 */
41 CSSProposalGeneratorForHTMLTag(CSSContentAssistContext context) {
42 super(context);
43
44 if (fHTMLTags == null) {
45 fHTMLTags = setupHTMLTags();
46 }
47 }
48
49 /**
50 * getCandidates method comment.
51 */
52 protected Iterator getCandidates() {
53 List candidates = new ArrayList();
54
55 if (checkLeadingColon()) {
56 return candidates.iterator();
57 }
58
59 // XHTML requires lower case
60 boolean bLowerCase = false;
61 if (fContext.getModel().getStyleSheetType() == ICSSModel.EMBEDDED) {
62 Node domNode = fContext.getModel().getOwnerDOMNode();
63 if (domNode != null && !(domNode instanceof Document)) {
64 domNode = domNode.getOwnerDocument();
david_williamsc39caaf2005-04-05 06:07:16 +000065 if (domNode instanceof IDOMDocument) {
66 DocumentTypeAdapter adapter = (DocumentTypeAdapter) ((IDOMDocument) domNode).getAdapterFor(DocumentTypeAdapter.class);
nitind958d79a2004-11-23 19:23:00 +000067 if (adapter != null)
68 bLowerCase = (adapter.getTagNameCase() == DocumentTypeAdapter.LOWER_CASE);
69 }
70 }
71 }
72
73
74 int length = fHTMLTags.length;
75 for (int i = 0; i < length; i++) {
76 String tagText = fHTMLTags[i];
77 if (bLowerCase) {
78 tagText = tagText.toLowerCase();
79 }
80 if (!isMatch(tagText)) {
81 continue;
82 }
83
84 int cursorPos = 0;
85 StringBuffer buf = new StringBuffer();
86 buf.append(tagText);
87 cursorPos += tagText.length();
88 boolean inRule = (fContext.getTargetNode() instanceof ICSSStyleRule);
89 if (!inRule || fContext.getTextToReplace().length() == 0) {
90 buf.append(" ");//$NON-NLS-1$
91 cursorPos += 1;
92 }
93 if (!inRule) {
94 StringAndOffset sao = generateBraces();
95 buf.append(sao.fString);
96 cursorPos += sao.fOffset;
97 }
98
99 CSSCACandidate item = new CSSCACandidate();
100 item.setReplacementString(buf.toString());
101 item.setCursorPosition(cursorPos);
102 item.setDisplayString(tagText);
103 item.setImageType(CSSImageType.SELECTOR_TAG);
104 candidates.add(item);
105 }
106
107 return candidates.iterator();
108 }
109
110 /**
111 *
112 */
113 private static String[] setupHTMLTags() {
114 CMDocument cmdoc = HTMLCMDocumentFactory.getCMDocument(CMDocType.HTML_DOC_TYPE);
115 CMNamedNodeMap elements = cmdoc.getElements();
116 Vector names = new Vector();
117 int nElements = elements.getLength();
118 for (int i = 0; i < nElements; i++) {
119 CMElementDeclaration edec = (CMElementDeclaration) elements.item(i);
120 if (isAttrDefined(edec, HTML40Namespace.ATTR_NAME_STYLE)) {
121 names.add(edec.getElementName());
122 }
123 }
124 Collections.sort(names);
125 String[] tags = new String[names.size()];
126 Iterator iNames = names.iterator();
127 for (int i = 0; iNames.hasNext(); i++) {
128 tags[i] = (String) iNames.next();
129 }
130 return tags;
131 }
132
133 /**
134 *
135 */
136 private static boolean isAttrDefined(CMElementDeclaration edec, String attrName) {
137 if (edec == null) {
138 return false;
139 }
140 CMNamedNodeMap attrs = edec.getAttributes();
141 if (attrs == null) {
142 return false;
143 }
144 for (int i = 0; i < attrs.getLength(); i++) {
145 CMAttributeDeclaration attr = (CMAttributeDeclaration) attrs.item(i);
146 if (attr == null) {
147 continue;
148 }
149 if (attr.getAttrName().equalsIgnoreCase(attrName)) {
150 return true;
151 }
152 }
153 return false;
154 }
155
156 private static String[] fHTMLTags = null;
157}