Skip to main content
summaryrefslogtreecommitdiffstats
blob: 880851941ce00784dd3f75afa1ceac219f86329e (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*******************************************************************************
 * Copyright (c) 2004, 2005 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.formatter;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.wst.css.core.internal.provisional.adapters.ICSSModelAdapter;
import org.eclipse.wst.css.core.internal.provisional.adapters.IStyleDeclarationAdapter;
import org.eclipse.wst.css.core.internal.provisional.adapters.IStyleSheetAdapter;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSModel;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSNode;
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
import org.w3c.dom.Node;
import org.w3c.dom.Text;


public class CSSFormatUtil {
	public List collectCSSNodes(IStructuredModel model, int start, int length) {
		List nodes = new ArrayList();

		IndexedRegion startNode = model.getIndexedRegion(start);
		IndexedRegion endNode = model.getIndexedRegion(start + length - 1);

		if (startNode == null || endNode == null) {
			return nodes;
		}

		if (model instanceof ICSSModel && startNode instanceof ICSSNode && endNode instanceof ICSSNode) {
			// CSS model
			ICSSNode ca = getCommonAncestor((ICSSNode) startNode, (ICSSNode) endNode);
			if (ca != null) {
				for (ICSSNode node = ca.getFirstChild(); node != null && start + length < ((IndexedRegion) node).getStartOffset(); node = node.getNextSibling()) {
					if (start < ((IndexedRegion) node).getEndOffset()) {
						nodes.add(node);
					}
				}
			}
		}
		else if (model instanceof IDOMModel && startNode instanceof IDOMNode && endNode instanceof IDOMNode) {
			if (startNode instanceof Text) {
				startNode = (IndexedRegion) ((Text) startNode).getParentNode();
			}
			if (endNode instanceof Text) {
				endNode = (IndexedRegion) ((Text) endNode).getParentNode();
			}
			// HTML model, maybe
			IDOMNode ca = (IDOMNode) getCommonAncestor((Node) startNode, (Node) endNode);
			findCSS(nodes, ca);
		}

		return nodes;
	}

	/**
	 * getCommonAncestor method
	 * 
	 * @return org.w3c.dom.Node
	 * @param node
	 *            org.w3c.dom.Node
	 */
	private Node getCommonAncestor(Node node1, Node node2) {
		if (node1 == null || node2 == null)
			return null;

		for (Node na = node2; na != null; na = na.getParentNode()) {
			for (Node ta = node1; ta != null; ta = ta.getParentNode()) {
				if (ta == na)
					return ta;
			}
		}
		return null; // not found
	}

	private void findCSS(List cssNodes, IDOMNode node) {
		ICSSModelAdapter adapter;
		adapter = (ICSSModelAdapter) node.getAdapterFor(IStyleSheetAdapter.class);
		if (adapter != null) {
			ICSSModel model = adapter.getModel();
			if (model != null && model.getStyleSheetType() == ICSSModel.EMBEDDED) {
				cssNodes.add(model.getDocument());
			}
		}
		else {
			adapter = (ICSSModelAdapter) node.getAdapterFor(IStyleDeclarationAdapter.class);
			if (adapter != null) {
				ICSSModel model = adapter.getModel();
				if (model != null && model.getStyleSheetType() == ICSSModel.INLINE) {
					cssNodes.add(model.getDocument());
				}
			}
		}

		for (IDOMNode child = (IDOMNode) node.getFirstChild(); child != null; child = (IDOMNode) child.getNextSibling()) {
			findCSS(cssNodes, child);
		}
	}

	private ICSSNode getCommonAncestor(ICSSNode nodeA, ICSSNode nodeB) {
		if (nodeA == null || nodeB == null) {
			return null;
		}

		for (ICSSNode na = nodeA; na != null; na = na.getParentNode()) {
			for (ICSSNode ta = nodeB; ta != null; ta = ta.getParentNode()) {
				if (ta == na) {
					return ta;
				}
			}
		}

		return null; // not found
	}

	/**
	 */
	public void replaceSource(IStructuredModel model, int offset, int length, String source) {
		if (model == null)
			return;
		IStructuredDocument structuredDocument = model.getStructuredDocument();
		if (structuredDocument == null)
			return;
		if (offset >= 0 && length >= 0 && offset + length <= structuredDocument.getLength()) {
			if (structuredDocument.containsReadOnly(offset, length))
				return;
			if (source == null)
				source = new String();
			// We use 'structuredDocument' as the requester object just so
			// this and the other
			// format-related 'repalceText' (in replaceSource) can use the
			// same requester.
			// Otherwise, if requester is not identical,
			// the undo group gets "broken" into multiple pieces based
			// on the requesters being different. Technically, any unique,
			// common
			// requester object would work.
			structuredDocument.replaceText(structuredDocument, offset, length, source);
		}
	}

	public synchronized static CSSFormatUtil getInstance() {
		if (fInstance == null) {
			fInstance = new CSSFormatUtil();
		}
		return fInstance;
	}

	private CSSFormatUtil() {
		super();
	}

	private static CSSFormatUtil fInstance;
}

Back to the top