nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 1 | /***************************************************************************** |
| 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 | ****************************************************************************/ |
| 9 | package org.eclipse.wst.css.ui.contentassist; |
| 10 | |
| 11 | |
| 12 | |
| 13 | import java.util.ArrayList; |
| 14 | import java.util.Collections; |
| 15 | import java.util.Iterator; |
| 16 | import java.util.List; |
| 17 | import java.util.Vector; |
| 18 | |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 19 | import org.eclipse.wst.css.core.document.ICSSModel; |
| 20 | import org.eclipse.wst.css.core.document.ICSSStyleRule; |
| 21 | import org.eclipse.wst.css.ui.image.CSSImageType; |
| 22 | import org.eclipse.wst.html.core.contentmodel.HTMLCMDocumentFactory; |
david_williams | c06c86f | 2005-03-18 18:23:41 +0000 | [diff] [blame^] | 23 | import org.eclipse.wst.xml.core.contentmodel.CMDocType; |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 24 | import org.eclipse.wst.xml.core.document.DocumentTypeAdapter; |
| 25 | import org.eclipse.wst.xml.core.document.XMLDocument; |
david_williams | c06c86f | 2005-03-18 18:23:41 +0000 | [diff] [blame^] | 26 | import org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration; |
| 27 | import org.eclipse.wst.xml.core.internal.contentmodel.CMDocument; |
| 28 | import org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration; |
| 29 | import org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap; |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 30 | import org.w3c.dom.Document; |
| 31 | import org.w3c.dom.Node; |
| 32 | |
| 33 | class 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(); |
| 65 | if (domNode instanceof XMLDocument) { |
| 66 | DocumentTypeAdapter adapter = (DocumentTypeAdapter) ((XMLDocument) domNode).getAdapterFor(DocumentTypeAdapter.class); |
| 67 | 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 | } |