001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.enterprise.xi.enhydrabarracuda;
016:
017: import java.lang.reflect.Method;
018: import java.lang.reflect.Modifier;
019: import java.util.StringTokenizer;
020:
021: /** This class contains some useful utilities for HTML processing */
022: public final class Util {
023: /** @return true if passed url looks like the local one. */
024: public static boolean isLocalUrl(String pUrl) {
025: if (pUrl == null)
026: return false;
027: // If URL has '/' character before it has :/ than it is local
028: int lSlashPos = pUrl.indexOf("/");
029: int lUrlProtocolPos = pUrl.indexOf("://");
030: if (lSlashPos < 0 && lUrlProtocolPos < 0)
031: return true; // No slashes - url is local
032: else if (lSlashPos > 0 && lUrlProtocolPos < 0)
033: return true; // No url protocol pos - url is local
034: return (lSlashPos < lUrlProtocolPos); // If there is a slash before UrlProtocol pos - the url is local
035: }
036:
037: /** Sets parameter to the given url and returns modified URL. */
038: public static String setURLParameter(String pUrl,
039: String pParameterName, String pParameterValue) {
040: String lParameterKey = pParameterName + "=";
041:
042: int lQuestionMarkPos = pUrl.indexOf("?");
043: if (lQuestionMarkPos < 0) {
044: // No parameters at all in this URL - just add the parameter
045: return pUrl + "?" + lParameterKey + pParameterValue;
046: }
047: // There are some parameters. Iterate through them one by one
048: // looking for one we want to replace
049: StringBuffer lStringBuffer = new StringBuffer(pUrl.substring(0,
050: lQuestionMarkPos + 1));
051: StringTokenizer lParametersTokenizer = new StringTokenizer(pUrl
052: .substring(lQuestionMarkPos + 1), "&", false);
053: boolean lInsertedParameter = false;
054: while (lParametersTokenizer.hasMoreTokens()) {
055: lStringBuffer.append("&");
056: String lParameterToken = lParametersTokenizer.nextToken();
057: if (lInsertedParameter == false
058: && lParameterToken.startsWith(lParameterKey)) {
059: lStringBuffer.append(lParameterKey);
060: lStringBuffer.append(pParameterValue);
061: lInsertedParameter = true;
062: } else {
063: lStringBuffer.append(lParameterToken);
064: }
065: }
066: // Here we may have a situation where the parameter was not found
067: if (!lInsertedParameter) {
068: lStringBuffer.append("&");
069: lStringBuffer.append(lParameterKey);
070: lStringBuffer.append(pParameterValue);
071: }
072: // That's it
073: return lStringBuffer.toString();
074: }
075:
076: /** Uses reflection to find document's element getter method */
077: public static Method getDocumentElementGettterMethod(
078: Class pDocumentClass, String pResourceId) {
079: // Look for getElement<ignorecase resource id>
080: String lMethodName = "getElement" + pResourceId;
081:
082: Method[] lMethods = pDocumentClass.getMethods();
083: for (int i = 0; i < lMethods.length; i++) {
084: Method lMethod = lMethods[i];
085: if (lMethod.getName().equalsIgnoreCase(lMethodName)) {
086: int lModifiers = lMethod.getModifiers();
087: if (Modifier.isPublic(lModifiers))
088: return lMethod;
089: }
090: }
091: return null;
092: }
093:
094: // /** Uses reflection to find widget's model creator method */
095: // public static Method getWidgetModelCreatorMethod(Class pWidgetClass)
096: // {
097: // // Look for WidgetModel createModel(HTMLElement.class)
098: // String lMethodName = "createModel";
099: // try
100: // {
101: // Method lMethod = pWidgetClass.getMethod("createModel",new Class[] {HTMLElement.class});
102: // int lModifiers = lMethod.getModifiers();
103: // if (lMethod.getReturnType().equals(WidgetModel.class) &&
104: // Modifier.isPublic(lModifiers) && Modifier.isStatic(lModifiers))
105: // return lMethod;
106: // return null;
107: // }
108: // catch(NoSuchMethodException e)
109: // {
110: // return null;
111: // }
112: // }
113: }
|