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.

summaryrefslogtreecommitdiffstats
blob: 860a4f9735f88193e4beef9720608f56fe7d2841 (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
/*******************************************************************************
 * 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.xml.ui.internal.taginfo;

import org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration;
import org.eclipse.wst.xml.core.internal.contentmodel.CMDataType;
import org.eclipse.wst.xml.core.internal.contentmodel.CMDocumentation;
import org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration;
import org.eclipse.wst.xml.core.internal.contentmodel.CMNode;
import org.eclipse.wst.xml.core.internal.contentmodel.CMNodeList;
import org.eclipse.wst.xml.core.internal.contentmodel.util.CMDescriptionBuilder;
import org.eclipse.wst.xml.ui.internal.XMLUIMessages;

/**
 * Provides basic tag information such as element/attribute name, data type,
 * and tag info/documentation for CMNodes. Uses HTML to enhance presentation.
 */
public class MarkupTagInfoProvider {
	protected final static String BOLD_END = "</b>"; //$NON-NLS-1$
	protected final static String BOLD_START = "<b>"; //$NON-NLS-1$
	protected final static String HEADING_END = "</h5>"; //$NON-NLS-1$
	protected final static String HEADING_START = "<h5>"; //$NON-NLS-1$
	protected final static String LIST_BEGIN = "<ul>"; //$NON-NLS-1$
	protected final static String LIST_ELEMENT = "<li>"; //$NON-NLS-1$
	protected final static String NEW_LINE = "<dl>"; //$NON-NLS-1$
	protected final static String PARAGRAPH_END = "</p>"; //$NON-NLS-1$
	protected final static String PARAGRAPH_START = "<p>"; //$NON-NLS-1$
	protected final static String SPACE = " "; //$NON-NLS-1$

	/**
	 * Returns basic tag information for display given a CMNode
	 *  
	 * @return String
	 */
	public String getInfo(CMNode node) {
		if (node == null)
			return null;
		StringBuffer sb = new StringBuffer();
		// we assume that if there is tagInfo present, only display tagInfo
		printTagInfo(sb, node);

		// no tagInfo present, so try to display tag description
		if (sb.length() == 0) {
			printDescription(sb, node);
		}

		// no tag description present either, so display default info
		if (sb.length() == 0) {
			printDefaultInfo(node, sb);
		}

		return sb.toString();
	}

	/**
	 * Adds dataType's data type information, including enumerated type values
	 * to string buffer, sb
	 * 
	 */
	protected void printDataTypeInfo(StringBuffer sb, CMDataType dataType) {
		String dataTypeName = dataType.getNodeName();
		if ((dataTypeName != null) && (dataTypeName.length() > 0)) {
			sb.append(PARAGRAPH_START + BOLD_START + XMLUIMessages.Data_Type____4 + SPACE + BOLD_END);
			sb.append(dataTypeName);
			sb.append(PARAGRAPH_END);
		}
		String[] enumeratedValue = dataType.getEnumeratedValues();
		if (enumeratedValue != null && enumeratedValue.length > 0) {
			sb.append(PARAGRAPH_START + BOLD_START + XMLUIMessages.Enumerated_Values____5 + SPACE + BOLD_END);
			sb.append(LIST_BEGIN);
			for (int i = 0; i < enumeratedValue.length; i++) {
				sb.append(LIST_ELEMENT + enumeratedValue[i]);
			}
			sb.append(PARAGRAPH_END);
		}
	}

	/**
	 * Adds the default info (element name, content model, data type) of
	 * CMNode to the string buffer, sb
	 * 
	 */
	protected void printDefaultInfo(CMNode node, StringBuffer sb) {
		{

			if (node.getNodeType() == CMNode.ELEMENT_DECLARATION) {
				CMElementDeclaration ed = (CMElementDeclaration) node;
				sb.append(PARAGRAPH_START + BOLD_START + XMLUIMessages.Element____1 + SPACE + BOLD_END);
				sb.append(node.getNodeName());
				sb.append(PARAGRAPH_END);
				if (ed.getContentType() == CMElementDeclaration.PCDATA) {
					CMDataType dataType = ed.getDataType();
					if (dataType != null) {
						printDataTypeInfo(sb, dataType);
					}
				} else {
					CMDescriptionBuilder builder = new CMDescriptionBuilder();
					String description = builder.buildDescription(node);
					if ((description != null) && (description.length() > 0)) {
						sb.append(PARAGRAPH_START + BOLD_START + XMLUIMessages.Content_Model____2 + SPACE + BOLD_END);
						sb.append(description + PARAGRAPH_END);
					}
				}
				printDocumentation(sb, node);
			} else if (node.getNodeType() == CMNode.ATTRIBUTE_DECLARATION) {
				CMAttributeDeclaration ad = (CMAttributeDeclaration) node;
				sb.append(PARAGRAPH_START + BOLD_START + XMLUIMessages.Attribute____3 + SPACE + BOLD_END);
				sb.append(node.getNodeName());
				sb.append(PARAGRAPH_END);
				CMDataType dataType = ad.getAttrType();
				if (dataType != null) {
					printDataTypeInfo(sb, dataType);
				}
				printDocumentation(sb, node);
			} else if (node.getNodeType() == CMNode.DATA_TYPE) {
				sb.append(PARAGRAPH_START + BOLD_START + XMLUIMessages.Data_Type____4 + SPACE + BOLD_END);
				sb.append(node.getNodeName());
				sb.append(PARAGRAPH_END);
				printDocumentation(sb, node);
			}
		}
	}

	/**
	 * Adds the description property of the CMNode to the string buffer, sb
	 * 
	 */
	protected void printDescription(StringBuffer sb, CMNode node) {
		String tagInfo = (String) node.getProperty("description"); //$NON-NLS-1$
		if (tagInfo != null) {
			sb.append(PARAGRAPH_START + tagInfo.trim() + PARAGRAPH_END);
		}
	}

	/**
	 * Adds the tag documentation property of the CMNode to the string buffer,
	 * sb
	 * 
	 */
	protected void printDocumentation(StringBuffer sb, CMNode node) {
		CMNodeList nodeList = (CMNodeList) node.getProperty("documentation"); //$NON-NLS-1$
		if ((nodeList != null) && (nodeList.getLength() > 0)) {
			sb.append(NEW_LINE);
			for (int i = 0; i < nodeList.getLength(); i++) {
				CMDocumentation documentation = (CMDocumentation) nodeList.item(i);
				String doc = documentation.getValue();
				if (doc != null) {
					sb.append(PARAGRAPH_START + doc.trim() + PARAGRAPH_END);
				}
			}
		}
	}

	/**
	 * Adds the tag info property of the CMNode to the string buffer, sb
	 * 
	 */
	protected void printTagInfo(StringBuffer sb, CMNode node) {
		String tagInfo = (String) node.getProperty("tagInfo"); //$NON-NLS-1$
		if (tagInfo != null) {
			sb.append(PARAGRAPH_START + tagInfo.trim() + PARAGRAPH_END);
		}
	}
}

Back to the top