blob: 50a5fc61275d3a626e0b407c528599f8e232545c [file] [log] [blame]
nitind958d79a2004-11-23 19:23:00 +00001/*******************************************************************************
amywu923ee602007-04-10 18:32:07 +00002 * Copyright (c) 2004, 2005 IBM Corporation and others.
nitind958d79a2004-11-23 19:23:00 +00003 * 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
amywu923ee602007-04-10 18:32:07 +00007 *
nitind958d79a2004-11-23 19:23:00 +00008 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
david_williams63219a22005-04-10 01:59:51 +000011package org.eclipse.wst.css.core.internal.util.declaration;
nitind958d79a2004-11-23 19:23:00 +000012
13
14
15import org.eclipse.wst.css.core.internal.contentmodel.PropCMProperty;
david_williams63219a22005-04-10 01:59:51 +000016import org.eclipse.wst.css.core.internal.parserz.CSSRegionContexts;
17import org.eclipse.wst.css.core.internal.parserz.CSSTextParser;
18import org.eclipse.wst.css.core.internal.parserz.CSSTextToken;
nitind958d79a2004-11-23 19:23:00 +000019
20
21/**
22 * For 'border-right' property 'border-right' will be expanded to
23 * 'border-right-color', 'border-right-style', 'border-right-width',
24 */
25public 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_williams63219a22005-04-10 01:59:51 +000057 else if (org.eclipse.wst.css.core.internal.util.CSSUtil.isLength(tokens[i]) || tokens[i].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_NUMBER) {
nitind958d79a2004-11-23 19:23:00 +000058 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_williams63219a22005-04-10 01:59:51 +0000109 else if (org.eclipse.wst.css.core.internal.util.CSSUtil.isLength(tokens[i]) || tokens[i].kind == CSSRegionContexts.CSS_DECLARATION_VALUE_NUMBER) {
nitind958d79a2004-11-23 19:23:00 +0000110 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 }
amywu923ee602007-04-10 18:32:07 +0000140}