nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
amywu | 923ee60 | 2007-04-10 18:32:07 +0000 | [diff] [blame] | 2 | * Copyright (c) 2004, 2005 IBM Corporation and others. |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 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 |
amywu | 923ee60 | 2007-04-10 18:32:07 +0000 | [diff] [blame] | 7 | * |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | *******************************************************************************/ |
david_williams | 63219a2 | 2005-04-10 01:59:51 +0000 | [diff] [blame] | 11 | package org.eclipse.wst.css.core.internal.util.declaration; |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 12 | |
| 13 | |
| 14 | |
| 15 | import org.eclipse.wst.css.core.internal.contentmodel.PropCMProperty; |
david_williams | 63219a2 | 2005-04-10 01:59:51 +0000 | [diff] [blame] | 16 | import org.eclipse.wst.css.core.internal.parserz.CSSRegionContexts; |
| 17 | import org.eclipse.wst.css.core.internal.parserz.CSSTextParser; |
| 18 | import org.eclipse.wst.css.core.internal.parserz.CSSTextToken; |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 19 | |
| 20 | |
| 21 | /** |
| 22 | * For 'border-right' property 'border-right' will be expanded to |
| 23 | * 'border-right-color', 'border-right-style', 'border-right-width', |
| 24 | */ |
| 25 | public class BorderRightShorthandAdapter implements IShorthandAdapter { |
| 26 | |
| 27 | /** |
| 28 | * |
| 29 | */ |
| 30 | public BorderRightShorthandAdapter() { |
| 31 | super(); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * |
| 36 | */ |
| 37 | public boolean expand(String source, CSSPropertyContext dest) { |
| 38 | CSSTextParser parser = new CSSTextParser(CSSTextParser.MODE_DECLARATION_VALUE, source); |
| 39 | CSSTextToken[] tokens = parser.getTokens(); |
| 40 | if (tokens.length <= 0) { |
| 41 | return false; |
| 42 | } |
| 43 | String color = "", style = "", width = "";//$NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$ |
| 44 | PropCMProperty propColor = PropCMProperty.getInstanceOf(PropCMProperty.P_BORDER_RIGHT_COLOR); |
| 45 | PropCMProperty propStyle = PropCMProperty.getInstanceOf(PropCMProperty.P_BORDER_RIGHT_STYLE); |
| 46 | PropCMProperty propWidth = PropCMProperty.getInstanceOf(PropCMProperty.P_BORDER_RIGHT_WIDTH); |
| 47 | |
| 48 | for (int i = 0; i < tokens.length; i++) { |
| 49 | if (tokens[i].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_IDENT) { |
| 50 | if (propStyle.canHave(tokens[i].image)) |
| 51 | style = tokens[i].image; |
| 52 | else if (propWidth.canHave(tokens[i].image)) |
| 53 | width = tokens[i].image; |
| 54 | else if (propColor.canHave(tokens[i].image)) |
| 55 | color = tokens[i].image; |
| 56 | } |
david_williams | 63219a2 | 2005-04-10 01:59:51 +0000 | [diff] [blame] | 57 | else if (org.eclipse.wst.css.core.internal.util.CSSUtil.isLength(tokens[i]) || tokens[i].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_NUMBER) { |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 58 | width = tokens[i].image; |
| 59 | } |
| 60 | else if (tokens[i].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_HASH) { |
| 61 | color = tokens[i].image; |
| 62 | } |
| 63 | else if (tokens[i].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_FUNCTION) { |
| 64 | StringBuffer buf = new StringBuffer(); |
| 65 | while (i < tokens.length) { |
| 66 | if (tokens[i].kind == CSSRegionContexts.CSS_COMMENT) { |
| 67 | i++; |
| 68 | continue; |
| 69 | } |
| 70 | buf.append(tokens[i].image); |
| 71 | if (tokens[i++].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_PARENTHESIS_CLOSE) |
| 72 | break; |
| 73 | } |
| 74 | i--; |
| 75 | color = buf.toString(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | dest.set(propColor.getName(), color); |
| 80 | dest.set(propStyle.getName(), style); |
| 81 | dest.set(propWidth.getName(), width); |
| 82 | |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * |
| 88 | */ |
| 89 | public String extract(String source, PropCMProperty propDest) { |
| 90 | CSSTextParser parser = new CSSTextParser(CSSTextParser.MODE_DECLARATION_VALUE, source); |
| 91 | CSSTextToken[] tokens = parser.getTokens(); |
| 92 | if (tokens.length <= 0) { |
| 93 | return null; |
| 94 | } |
| 95 | String color = null, style = null, width = null; |
| 96 | PropCMProperty propColor = PropCMProperty.getInstanceOf(PropCMProperty.P_BORDER_RIGHT_COLOR); |
| 97 | PropCMProperty propStyle = PropCMProperty.getInstanceOf(PropCMProperty.P_BORDER_RIGHT_STYLE); |
| 98 | PropCMProperty propWidth = PropCMProperty.getInstanceOf(PropCMProperty.P_BORDER_RIGHT_WIDTH); |
| 99 | |
| 100 | for (int i = 0; i < tokens.length; i++) { |
| 101 | if (tokens[i].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_IDENT) { |
| 102 | if (propStyle.canHave(tokens[i].image)) |
| 103 | style = tokens[i].image; |
| 104 | else if (propWidth.canHave(tokens[i].image)) |
| 105 | width = tokens[i].image; |
| 106 | else if (propColor.canHave(tokens[i].image)) |
| 107 | color = tokens[i].image; |
| 108 | } |
david_williams | 63219a2 | 2005-04-10 01:59:51 +0000 | [diff] [blame] | 109 | else if (org.eclipse.wst.css.core.internal.util.CSSUtil.isLength(tokens[i]) || tokens[i].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_NUMBER) { |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 110 | width = tokens[i].image; |
| 111 | } |
| 112 | else if (tokens[i].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_HASH) { |
| 113 | color = tokens[i].image; |
| 114 | } |
| 115 | else if (tokens[i].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_FUNCTION) { |
| 116 | StringBuffer buf = new StringBuffer(); |
| 117 | while (i < tokens.length) { |
| 118 | if (tokens[i].kind == CSSRegionContexts.CSS_COMMENT) { |
| 119 | i++; |
| 120 | continue; |
| 121 | } |
| 122 | buf.append(tokens[i].image); |
| 123 | if (tokens[i++].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_PARENTHESIS_CLOSE) |
| 124 | break; |
| 125 | } |
| 126 | i--; |
| 127 | color = buf.toString(); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | if (propColor == propDest) |
| 132 | return color; |
| 133 | else if (propStyle == propDest) |
| 134 | return style; |
| 135 | else if (propWidth == propDest) |
| 136 | return width; |
| 137 | else |
| 138 | return null; |
| 139 | } |
amywu | 923ee60 | 2007-04-10 18:32:07 +0000 | [diff] [blame] | 140 | } |