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: 6c93c5d62f54de8be63090776b680b7b6ad0a447 (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
/*******************************************************************************
 * Copyright (c) 2001, 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
 *     Jens Lukowski/Innoopract - initial renaming/restructuring
 *     
 *******************************************************************************/
package org.eclipse.wst.dtd.core.internal;

import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;

// base class for an Element's contentmodel
public abstract class CMNode extends DTDNode {
	public static final String ANY = DTDCoreMessages._UI_LABEL_CM_NODE_ANY; //$NON-NLS-1$
	public static final String CHILDREN = DTDCoreMessages._UI_LABEL_CM_NODE_CHILD_CONTENT; //$NON-NLS-1$

	public static final String EMPTY = DTDCoreMessages._UI_LABEL_CM_NODE_EMPTY; //$NON-NLS-1$
	public static final String MIXED = DTDCoreMessages._UI_LABEL_CM_NODE_MIX_CONTENT; //$NON-NLS-1$
	public static final String PCDATA = DTDCoreMessages._UI_LABEL_CM_NODE_PCDATA; //$NON-NLS-1$


	boolean rootElementContent;

	public CMNode(DTDFile file, IStructuredDocumentRegion flatNode) {
		super(file, flatNode);
	}

	// this is only valid to ask if the content is a root element content
	abstract public String getType();

	/**
	 * Get the value of rootElementContent. This tells us whether this element
	 * content's parent is a direct decendent of the containing element
	 * 
	 * @return value of rootElementContent.
	 */
	public boolean isRootElementContent() {
		return rootElementContent;
	}

	// if this is a root element, change the content to children
	// ie . (child1)
	public void setChildrenContent(String newChild) {
		if (isRootElementContent()) {
			if (!newChild.equals("")) { //$NON-NLS-1$
				beginRecording(this, DTDCoreMessages._UI_LABEL_CM_NODE_SET_CHILD_CONTENT); //$NON-NLS-1$
				replaceText(this, getStartOffset(), getNodeLength(), "(" + newChild + ")"); //$NON-NLS-1$ //$NON-NLS-2$
				endRecording(this);
				return;
			}

			if (!getType().equals(CHILDREN)) {
				beginRecording(this, DTDCoreMessages._UI_LABEL_CM_NODE_SET_CHILD_CONTENT); //$NON-NLS-1$
				if (this instanceof CMBasicNode) {
					replaceText(this, getStartOffset(), getNodeLength(), "(newChild)"); //$NON-NLS-1$
				}
				else {
					// now must convert from mixed content to this one. must
					// preserve the remaining children
					CMGroupNode group = (CMGroupNode) this;
					CMNode firstChild = (CMNode) group.getFirstChild();
					if (firstChild.getType().equals(PCDATA)) {
						group.delete(firstChild);
					}
				}

				endRecording(this);
			}
		}
	}

	public void setContent(String content) {
		if (isRootElementContent()) {
			beginRecording(this, DTDCoreMessages._UI_LABEL_CM_NODE_SET + " " + content + " " + DTDCoreMessages._UI_LABEL_CM_NODE_CONTENT); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
			replaceText(this, getStartOffset(), getNodeLength(), content);
			endRecording(this);
		}
	}

	// if this is a root element, change the content to mixed
	// ie . (#PCDATA, child1)
	public void setMixedContent() {
		if (isRootElementContent()) {
			if (!getType().equals(MIXED)) {
				beginRecording(this, DTDCoreMessages._UI_LABEL_CM_NODE_SET_MIX_CONTENT); //$NON-NLS-1$
				if (this instanceof CMBasicNode) {
					replaceText(this, getStartOffset(), getNodeLength(), "(#PCDATA | newChild)*"); //$NON-NLS-1$
				}
				else {
					// now must convert from children content to this one.
					// must
					// preserve the children
					CMGroupNode group = (CMGroupNode) this;
					group.setConnector(CMGroupNode.CHOICE);
					group.setOccurrence(CMRepeatableNode.ZERO_OR_MORE);
					CMNode firstChild = (CMNode) group.getFirstChild();
					if (!firstChild.getType().equals(PCDATA)) {
						group.insertChildNode("#PCDATA", 0); //$NON-NLS-1$
					}
				}
				endRecording(this);
			}
		}
	}

	/**
	 * Set the value of rootElementContent.
	 * 
	 * @param v
	 *            Value to assign to rootElementContent.
	 */
	public void setRootElementContent(boolean v) {
		this.rootElementContent = v;
	}

	// public void delete()
	// {
	// if (isRootElementContent())
	// {
	// // then the superclasses delete will be fine
	// super.delete();
	// return;
	// }

	// CMGroupNode parent = (CMGroupNode) getParentNode();
	// parent.removeChildNode(this);
	// }


}

Back to the top