nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2004 IBM Corporation and others. |
| 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | *******************************************************************************/ |
david_williams | aaadf2b | 2005-04-11 07:28:31 +0000 | [diff] [blame] | 11 | package org.eclipse.wst.html.core.internal.htmlcss; |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 12 | |
| 13 | |
| 14 | |
amywu | fecd7a9 | 2006-05-10 07:22:58 +0000 | [diff] [blame] | 15 | import com.ibm.icu.util.StringTokenizer; |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 16 | |
david_williams | 63219a2 | 2005-04-10 01:59:51 +0000 | [diff] [blame] | 17 | import org.eclipse.wst.css.core.internal.provisional.adapters.IStyleSelectorAdapter; |
| 18 | import org.eclipse.wst.css.core.internal.provisional.document.ICSSSimpleSelector; |
david_williams | 4ad020f | 2005-04-18 08:00:30 +0000 | [diff] [blame] | 19 | import org.eclipse.wst.sse.core.internal.provisional.INodeNotifier; |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 20 | import org.w3c.dom.Element; |
| 21 | |
| 22 | /** |
| 23 | * Insert the type's description here. |
| 24 | */ |
| 25 | public class HTMLStyleSelectorAdapter implements IStyleSelectorAdapter { |
| 26 | |
| 27 | static private HTMLStyleSelectorAdapter instance; |
| 28 | private Object toMatch = IStyleSelectorAdapter.class; |
| 29 | |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 30 | public synchronized static HTMLStyleSelectorAdapter getInstance() { |
| 31 | if (instance == null) { |
| 32 | instance = new HTMLStyleSelectorAdapter(); |
| 33 | } |
| 34 | return instance; |
| 35 | } |
| 36 | |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 37 | public boolean isAdapterForType(Object type) { |
| 38 | return type == toMatch; |
| 39 | } |
| 40 | |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 41 | public boolean match(ICSSSimpleSelector selector, Element element, String pseudoName) { |
| 42 | if (element == null) |
| 43 | return false; |
| 44 | int i; |
| 45 | String key; |
| 46 | |
| 47 | // PseudoName |
| 48 | i = selector.getNumOfPseudoNames(); |
| 49 | if (i > 0) { |
| 50 | if (pseudoName == null || pseudoName.length() == 0) |
| 51 | return false; |
| 52 | for (i = i - 1; i >= 0; i--) { |
| 53 | if (pseudoName.equalsIgnoreCase(selector.getPseudoName(i))) { |
| 54 | break; |
| 55 | } |
| 56 | } |
| 57 | if (i < 0) |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | // check tag name |
| 62 | if (!selector.isUniversal() && !element.getNodeName().equalsIgnoreCase(selector.getName())) |
| 63 | return false; |
| 64 | |
| 65 | // check id |
| 66 | i = selector.getNumOfIDs(); |
| 67 | if (i > 0) { |
| 68 | if (i > 1) |
| 69 | return false; |
| 70 | key = element.getAttribute("id");//$NON-NLS-1$ |
| 71 | if (key == null) |
| 72 | return false; |
| 73 | if (!selector.getID(0).equals(key)) |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | // check class |
| 78 | i = selector.getNumOfClasses(); |
| 79 | if (i > 0) { |
| 80 | key = element.getAttribute("class");//$NON-NLS-1$ |
| 81 | if (key == null) |
| 82 | return false; |
| 83 | StringTokenizer tokenizer = new StringTokenizer(key); |
| 84 | for (i = i - 1; i >= 0; i--) { |
| 85 | boolean ok = false; |
| 86 | while (tokenizer.hasMoreTokens()) { |
| 87 | if (selector.getClass(i).equals(tokenizer.nextToken())) { |
| 88 | ok = true; |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | if (!ok) |
| 93 | return false; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // check attributes |
| 98 | for (i = selector.getNumOfAttributes() - 1; i >= 0; i--) { |
| 99 | StringTokenizer tokenizer = new StringTokenizer(selector.getAttribute(i), "=~| \t\r\n\f");//$NON-NLS-1$ |
| 100 | int countTokens = tokenizer.countTokens(); |
| 101 | if (countTokens > 0) { |
| 102 | String attrValue = element.getAttribute(tokenizer.nextToken()); |
| 103 | if (attrValue == null) |
| 104 | return false; |
| 105 | if (countTokens > 1) { |
| 106 | String token = tokenizer.nextToken("= \t\r\n\f");//$NON-NLS-1$ |
| 107 | StringTokenizer attrValueTokenizer = null; |
| 108 | if (token.equals("~")) {//$NON-NLS-1$ |
| 109 | attrValueTokenizer = new StringTokenizer(attrValue); |
| 110 | } |
| 111 | else if (token.equals("|")) {//$NON-NLS-1$ |
| 112 | attrValueTokenizer = new StringTokenizer(attrValue, "-");//$NON-NLS-1$ |
| 113 | } |
| 114 | if (attrValueTokenizer != null) { |
| 115 | if (tokenizer.hasMoreTokens()) { |
| 116 | token = tokenizer.nextToken(); |
| 117 | boolean ok = false; |
| 118 | while (attrValueTokenizer.hasMoreTokens()) { |
| 119 | if (token.equals(attrValueTokenizer.nextToken())) { |
| 120 | ok = true; |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | if (!ok) |
| 125 | return false; |
| 126 | } |
| 127 | } |
| 128 | else { |
| 129 | if (!attrValue.equals(token)) |
| 130 | return false; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | return true; |
| 137 | } |
| 138 | |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 139 | public void notifyChanged(INodeNotifier notifier, int eventType, Object changedFeature, Object oldValue, Object newValue, int pos) { |
| 140 | } |
| 141 | } |