001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.tests.performance;
011:
012: import java.io.IOException;
013: import java.io.InputStream;
014: import java.io.Reader;
015:
016: import org.eclipse.core.runtime.QualifiedName;
017: import org.eclipse.core.runtime.content.IContentDescriber;
018: import org.eclipse.core.runtime.content.IContentDescription;
019: import org.eclipse.core.runtime.content.ITextContentDescriber;
020:
021: /**
022: * This test ContentDescriber added specifically for OpenNavigatorFolderTest.
023: *
024: */
025:
026: public final class ContentDescriberForTestsOnly implements
027: ITextContentDescriber {
028:
029: /*
030: * this "simulated time" represents how long a content describer might
031: * take parsing a file. Offhand (i.e. just guessing) 2000 msecs would be "bad case" examples.
032: * 20 msecs would be near a good, optimistic case.
033: * I've picked 75 msecs for this test, to represent "normal" case
034: * (such as the HTML parser that gave rise to the original bug :)
035: *
036: * Note: unlike a "real" ContentDescriber, this one only effects "elapsed
037: * time", not "CPU Time", since all it is doing is sleeping.
038: */
039: private static final int SIMULATED_CALCULATION_TIME = 75;
040: private static final QualifiedName[] SUPPORTED_OPTIONS = {
041: IContentDescription.CHARSET,
042: IContentDescription.BYTE_ORDER_MARK };
043:
044: public int describe(InputStream contents,
045: IContentDescription description) throws IOException {
046: int result = IContentDescriber.INDETERMINATE;
047:
048: if (description == null) {
049: result = computeValidity(contents);
050: } else {
051: calculateSupportedOptions(contents, description);
052: // assummming we should return same 'validity' value we did
053: // when called before. (technically, could be a performance issue
054: // in future, so might want to check if any 'ol value would
055: // be ok here.
056: result = computeValidity(contents);
057: }
058:
059: return result;
060: }
061:
062: public int describe(Reader contents, IContentDescription description)
063: throws IOException {
064: int result = IContentDescriber.INDETERMINATE;
065:
066: if (description == null) {
067: result = computeValidity(contents);
068: } else {
069: calculateSupportedOptions(contents, description);
070: // assummming we should return same 'validity' value we did
071: // when called before. (technically, could be a performance issue
072: // in future, so might want to check if hard coded 'valid' would
073: // be ok here.
074: result = computeValidity(contents);
075: }
076:
077: return result;
078: }
079:
080: public QualifiedName[] getSupportedOptions() {
081:
082: return SUPPORTED_OPTIONS;
083: }
084:
085: private void calculateSupportedOptions(InputStream contents,
086: IContentDescription description) throws IOException {
087: if (isRelevent(description)) {
088: makeBusy();
089: }
090: }
091:
092: private void makeBusy() {
093: // for this test "content type", we don't
094: // really calculate anything. We just
095: // just take a long time doing nothing.
096: try {
097: Thread.sleep(SIMULATED_CALCULATION_TIME);
098: } catch (InterruptedException e) {
099: // never expected
100: e.printStackTrace();
101: }
102: }
103:
104: /**
105: * @param contents
106: * @param description
107: * @throws IOException
108: */
109: private void calculateSupportedOptions(Reader contents,
110: IContentDescription description) throws IOException {
111: if (isRelevent(description)) {
112: makeBusy();
113: }
114: }
115:
116: private int computeValidity(InputStream inputStream) {
117: // currently no contents specific check for valid HTML contents
118: // (this may change once we add XHTML content type)
119: return IContentDescriber.INDETERMINATE;
120: }
121:
122: private int computeValidity(Reader reader) {
123: // no contents specific check for valid HTML contents
124: // (this may change once we add XHTML content type)
125: return IContentDescriber.INDETERMINATE;
126: }
127:
128: /**
129: * @param description
130: * @return
131: */
132: private boolean isRelevent(IContentDescription description) {
133: boolean result = false;
134: if (description == null)
135: result = false;
136: else if (description
137: .isRequested(IContentDescription.BYTE_ORDER_MARK))
138: result = true;
139: else if (description.isRequested(IContentDescription.CHARSET))
140: result = true;
141: return result;
142: }
143:
144: }
|