Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3c10322468991f7ccc3595638733dfc0002c7b84 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*******************************************************************************
 * Copyright (c) 2001, 2006 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
 *     Jens Lukowski/Innoopract - initial renaming/restructuring
 *     
 *******************************************************************************/
package org.eclipse.wst.dtd.core.internal;

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

import org.eclipse.swt.graphics.Image;
import org.eclipse.wst.dtd.core.internal.parser.DTDRegionTypes;
import org.eclipse.wst.dtd.core.internal.text.RegionIterator;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion;
import org.w3c.dom.Node;


public class Element extends NamedTopLevelNode {

	List attListList = new ArrayList();

	List attributes = new ArrayList();

	protected CMNode fContentModel;

	public Element(DTDFile dtdFile, IStructuredDocumentRegion flatNode) {
		super(dtdFile, flatNode, DTDRegionTypes.ELEMENT_TAG);
	}

	public void addAttribute(String name) {
		beginRecording(this, DTDCoreMessages._UI_LABEL_ELEMENT_ADD_ATTR); //$NON-NLS-1$
		List attLists = getAttributeLists();
		if (attLists.size() == 0) {
			getDTDFile().createAttributeList(this, getName(), true);
			attLists = getAttributeLists();
		}
		if (attLists.size() > 0) {
			AttributeList attList = (AttributeList) attLists.get(attLists.size() - 1);
			attList.addAttribute(name);
		}
		endRecording(this);
	}

	public void addChild() {
		beginRecording(this, DTDCoreMessages._UI_LABEL_ELEMENT_ADD_CHILD); //$NON-NLS-1$
		addContent(this, " EMPTY"); //$NON-NLS-1$
		endRecording(this);
	}

	protected void addContent(Object requestor, String content) {
		ITextRegion whitespace = getWhitespaceAfterName();
		int startOffset = 0;
		int length = 0;
		if (whitespace != null) {
			startOffset = getStructuredDTDDocumentRegion().getStartOffset(whitespace);
			length = whitespace.getLength() >= 2 ? 1 : 0;
		}
		else {
			ITextRegion nameRegion = getNameRegion();
			if (nameRegion != null) {
				startOffset = getStructuredDTDDocumentRegion().getEndOffset(nameRegion);
			}
			else {
				ITextRegion elementTag = getNextRegion(iterator(), DTDRegionTypes.ELEMENT_TAG);
				startOffset = getStructuredDTDDocumentRegion().getEndOffset(elementTag);
			}
		}
		replaceText(requestor, startOffset, length, content);
	}

	public void addGroup() {
		beginRecording(this, DTDCoreMessages._UI_LABEL_ELEMENT_ADD_GRP); //$NON-NLS-1$
		addContent(this, " ()"); //$NON-NLS-1$
		endRecording(this);
	}

	public Node cloneNode(boolean deep) {
		return new Element(dtdFile, flatNode);
	}

	public List getAttributeLists() {
		attListList.clear();
		String elementName = getName();
		Iterator iter = dtdFile.getNodes().iterator();
		while (iter.hasNext()) {
			DTDNode node = (DTDNode) iter.next();
			if (node instanceof AttributeList && node.getName().equals(elementName)) {
				attListList.add(node);
			}
		}
		return attListList;
	}



	public CMNode getContentModel() {
		// Object[] children = getChildren()
		return (CMNode) getFirstChild();// contentModel;
	}

	public List getElementAttributes() {
		attributes.clear();
		Iterator attLists = getAttributeLists().iterator();
		while (attLists.hasNext()) {
			AttributeList attList = (AttributeList) attLists.next();

			Object[] children = attList.getChildren();
			for (int i = 0; i < children.length; i++) {
				attributes.add(children[i]);
			}
		}
		return attributes;
	}

	public Image getImage() {
		return DTDCorePlugin.getInstance().getImage(DTDResource.ELEMENTICON);
	}

	public short getNodeType() {
		return Node.ELEMENT_NODE;
	}

	public void replaceContentModel(Object requestor, CMNode node) {
		replaceContentModel(requestor, node.getNodeText());
	}

	public void replaceContentModel(Object requestor, String nodeText) {
		int offset = 0;
		int length = 0;
		CMNode contentModel = getContentModel();
		if (contentModel != null) {
			offset = contentModel.getStartOffset();
			length = contentModel.getWhitespaceEndOffset() - offset;
			replaceText(requestor, offset, length, nodeText);
		}
		else {
			addContent(requestor, nodeText);
		}
	}

	public void resolveRegions() {
		// System.out.println("element node stream = " +
		// tokenStream.getString());
		fContentModel = null;
		removeChildNodes();
		RegionIterator iter = iterator();

		if (getNameRegion() != null) {
			// we skip past the name token is our name
			skipPastName(iter);
		}

		while (iter.hasNext()) {
			ITextRegion currentRegion = iter.next();

			if (fContentModel == null) {
				if (currentRegion.getType().equals(DTDRegionTypes.NAME)) {
					fContentModel = new CMBasicNode(getDTDFile(), getStructuredDTDDocumentRegion());
				}
				else if (currentRegion.getType().equals(DTDRegionTypes.CONTENT_PCDATA)) {
					fContentModel = new CMBasicNode(getDTDFile(), getStructuredDTDDocumentRegion());
				}
				else if (currentRegion.getType().equals(DTDRegionTypes.LEFT_PAREN)) {
					fContentModel = new CMGroupNode(getDTDFile(), getStructuredDTDDocumentRegion());
				}
			}

			if (fContentModel != null) {
				if (!currentRegion.getType().equals(DTDRegionTypes.END_TAG)) {
					// content model gets all regions except for the '>'
					fContentModel.addRegion(currentRegion);
				}
				else {
					// if it is equal to the end tag, then don't add anymore
					// regions
					// for the content model
					break;
				}

			}

		}
		if (fContentModel != null) {
			appendChild(fContentModel);
			// this is the root element content so set it true
			fContentModel.setRootElementContent(true);
			// now tell the content model to resolve it's regions
			fContentModel.resolveRegions();

		}
	}

	public void setContentModel(CMNode contentModel) {
		this.fContentModel = contentModel;
	}
}

Back to the top