Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

aboutsummaryrefslogtreecommitdiffstats
blob: aad0b1b1bd2f2e7f9d6f011570ce3235209b3b89 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*******************************************************************************
 * Copyright (c) 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.css.core.internal.metamodel.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.wst.css.core.internal.metamodel.CSSMMDescriptor;
import org.eclipse.wst.css.core.internal.metamodel.CSSMMNode;
import org.eclipse.wst.css.core.internal.metamodel.CSSMMProperty;
import org.eclipse.wst.css.core.internal.metamodel.CSSMetaModel;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSNode;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclItem;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclaration;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleRule;
import org.w3c.dom.css.CSSFontFaceRule;


public class CSSMetaModelUtil {
	public CSSMetaModelUtil(CSSMetaModel metamodel) {
		super();
		fMetaModel = metamodel;
	}

	public Iterator getProperties() {
		return collectNodesByType(CSSMMNode.TYPE_PROPERTY);
	}

	public Iterator getDescriptors() {
		return collectNodesByType(CSSMMNode.TYPE_DESCRIPTOR);
	}

	public Iterator collectNodesByType(String type) {
		CSSMMTypeCollector collector = new CSSMMTypeCollector();
		collector.apply(fMetaModel, type);
		return collector.getNodes();
	}

	public CSSMMProperty getProperty(String propertyName) {
		Iterator iProperty = getProperties();
		while (iProperty.hasNext()) {
			CSSMMNode node = (CSSMMNode) iProperty.next();
			if (node.getName().equalsIgnoreCase(propertyName)) {
				return (CSSMMProperty) node;
			}
		}
		return null;
	}

	public CSSMMDescriptor getDescriptor(String descriptorName) {
		Iterator iDescriptor = getDescriptors();
		while (iDescriptor.hasNext()) {
			CSSMMNode node = (CSSMMNode) iDescriptor.next();
			if (node.getName().equalsIgnoreCase(descriptorName)) {
				return (CSSMMDescriptor) node;
			}
		}
		return null;
	}

	public CSSMMNode getMetaModelNodeFor(ICSSNode node) {
		if (node instanceof ICSSStyleDeclaration) {
			node = node.getParentNode();
		}
		if (node instanceof ICSSStyleDeclItem) {
			ICSSNode parent = node.getParentNode();
			if (parent != null) {
				parent = parent.getParentNode();
			}
			if (parent instanceof ICSSStyleRule) {
				return getProperty(((ICSSStyleDeclItem) node).getPropertyName());
			}
			else if (parent instanceof CSSFontFaceRule) {
				return getDescriptor(((ICSSStyleDeclItem) node).getPropertyName());
			}
		}
		if (node == null) {
			return null;
		}

		if (fTypeMap == null) {
			fTypeMap = new HashMap();
			fTypeMap.put(new Short(ICSSNode.STYLERULE_NODE), CSSMMNode.TYPE_STYLE_RULE);
			fTypeMap.put(new Short(ICSSNode.FONTFACERULE_NODE), CSSMMNode.TYPE_FONT_FACE_RULE);
			fTypeMap.put(new Short(ICSSNode.PAGERULE_NODE), CSSMMNode.TYPE_PAGE_RULE);
		}

		String nodeType = (String) fTypeMap.get(new Short(node.getNodeType()));
		if (nodeType == null) {
			return null;
		}

		Iterator iNodes = collectNodesByType(nodeType);
		if (iNodes.hasNext()) {
			CSSMMNode targetNode = (CSSMMNode) iNodes.next();
			if (!iNodes.hasNext()) { // it's only one
				return targetNode;
			}
		}

		return null;
	}

	public CSSMetaModel getMetaModel() {
		return fMetaModel;
	}


	private CSSMetaModel fMetaModel = null;
	private Map fTypeMap = null;
}

Back to the top