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: 975100a01363aa1008c40fc26dc8875f271552f9 (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
/*******************************************************************************
 * 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.sse.core.internal.util;



import java.io.BufferedInputStream;
import java.io.InputStream;

import org.eclipse.wst.sse.core.internal.encoding.CodedIO;
import org.eclipse.wst.sse.core.internal.encoding.util.BufferedLimitedStream;



public class Utilities {

	/**
	 * a common calculation in some of the parsing methods (e.g. in
	 * ContextRegion and IStructuredDocumentRegion)
	 */
	public static int calculateLengthDifference(String changes, int lengthToReplace) {
		// determine the length by the text itself, or, if there is no text to
		// insert (i.e. we are doing a delete) then calculate the length as
		// a negative number to denote the amount to delete.
		// For a straight insert, the selection Length will be zero.
		int lengthDifference = 0;
		if (changes == null) {
			// the delete case
			lengthDifference = 0 - lengthToReplace;
		}
		else {
			lengthDifference = changes.length() - lengthToReplace;
		}
		if (Debug.debugStructuredDocument) {
			System.out.println("lengthDifference: " + lengthDifference);//$NON-NLS-1$
		}
		return lengthDifference;
	}

	/**
	 * Returns true iff both parameters are not null and the object is within
	 * the array. Careful, this uses identity. Not good for basic strings.
	 */
	public static boolean contains(Object[] objectArray, Object object) {
		boolean result = false;
		// if object or objectArray is null, return false
		if ((objectArray != null) && (object != null)) {
			for (int i = 0; i < objectArray.length; i++) {
				if (objectArray[i] == object) {
					result = true;
					break;
				}
			}
		}
		return result;
	}

	public static boolean containsString(String[] objectArray, String object) {
		boolean result = false;
		// if object or objectArray is null, return false
		if ((objectArray != null) && (object != null)) {
			for (int i = 0; i < objectArray.length; i++) {
				if (objectArray[i].equals(object)) {
					result = true;
					break;
				}
			}
		}
		return result;
	}

	/**
	 * Ensures that an InputStream has mark/reset support, is readlimit is
	 * set, and that the stream is "limitable" (that is, reports "end of
	 * input" rather than allow going past mark). This is very specialized
	 * stream introduced to overcome
	 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=67211. See also
	 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=68565
	 */
	public static InputStream getLimitedStream(InputStream original) {
		if (original == null)
			return null;
		if (original instanceof BufferedLimitedStream)
			return original;
		return new BufferedLimitedStream(original, CodedIO.MAX_BUF_SIZE);
	}

	/**
	 * <p>
	 * Ensures that an InputStream has mark/reset support.
	 * </p>
	 * <p>
	 * It's vital that a BufferedInputStream <b>not</b> be wrapped in another
	 * BufferedInputStream as each can preemptively consume <i>n</i> bytes
	 * (e.g. 2048) from the parent stream before any requests are made. The
	 * cascading effect is that the second/inner BufferedInputStream can never
	 * rewind itself to the first <i>n</i> bytes since they were already
	 * consumed by its parent.
	 * </p>
	 */
	public static InputStream getMarkSupportedStream(InputStream original) {
		if (original == null)
			return null;
		if (original.markSupported())
			return original;
		InputStream buffered = new BufferedInputStream(original, CodedIO.MAX_BUF_SIZE);
		buffered.mark(CodedIO.MAX_MARK_SIZE);
		return buffered;
	}

	/**
	 * Used for log/trace messages. Id is assumed to be some form of a
	 * filename. See IModelManager.
	 */
	public static String makeShortId(Object id) {
		if (id == null)
			id = "NOID";//$NON-NLS-1$
		String whole = id.toString();
		String part = whole.substring(whole.lastIndexOf("/") + 1); //$NON-NLS-1$
		return "..." + part; //$NON-NLS-1$
	}


	/**
	 * Utilities constructor comment.
	 */
	public Utilities() {
		super();
	}
}

Back to the top