Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2011, FuseSource Corp. All rights reserved. http://fusesource.com The software in this package is published under the terms of the CDDL license a copy of which has been included with this distribution in the license.txt file.
  
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.fusesource.fabric.dosgi.util;
 
 import java.net.URI;
 import java.util.List;
 import java.util.Map;

 
 public class URISupport {
 
     public static class CompositeData {
         private String host;
         private String scheme;
         private String path;
         private URI components[];
         private Map<StringStringparameters;
         private String fragment;
 
         public URI[] getComponents() {
             return ;
         }
 
         public String getFragment() {
             return ;
         }
 
         public Map<StringStringgetParameters() {
             return ;
         }
 
         public String getScheme() {
             return ;
         }
 
         public String getPath() {
             return ;
         }
 
         public String getHost() {
             return ;
         }
 
         public URI toURI() throws URISyntaxException {
             StringBuffer sb = new StringBuffer();
             if ( != null) {
                 sb.append();
                 sb.append(':');
             }
 
             if ( != null && .length() != 0) {
                 sb.append();
             } else {
                 sb.append('(');
                 for (int i = 0; i < .i++) {
                     if (i != 0) {
                         sb.append(',');
                     }
                     sb.append([i].toString());
                 }
                 sb.append(')');
             }
 
             if ( != null) {
                 sb.append('/');
                 sb.append();
             }
             if (!.isEmpty()) {
                 sb.append("?");
                sb.append(createQueryString());
            }
            if ( != null) {
                sb.append("#");
                sb.append();
            }
            return new URI(sb.toString());
        }
    }
    public static Map<StringStringparseQuery(String urithrows URISyntaxException {
        try {
            Map<StringStringrc = new HashMap<StringString>();
            if (uri != null) {
                String[] parameters = uri.split("&");
                for (int i = 0; i < parameters.lengthi++) {
                    int p = parameters[i].indexOf("=");
                    if (p >= 0) {
                        String name = URLDecoder.decode(parameters[i].substring(0, p), "UTF-8");
                        String value = URLDecoder.decode(parameters[i].substring(p + 1), "UTF-8");
                        rc.put(namevalue);
                    } else {
                        rc.put(parameters[i], null);
                    }
                }
            }
            return rc;
        } catch (UnsupportedEncodingException e) {
            throw (URISyntaxException)new URISyntaxException(e.toString(), "Invalid encoding").initCause(e);
        }
    }
    public static Map<StringStringparseParameters(URI urithrows URISyntaxException {
        return uri.getQuery() == null ? emptyMap() : parseQuery(stripPrefix(uri.getQuery(), "?"));
    }
    @SuppressWarnings("unchecked")
    private static Map<StringStringemptyMap() {
        return .;
    }

    
Removes any URI query from the given uri
    public static URI removeQuery(URI urithrows URISyntaxException {
        return createURIWithQuery(urinull);
    }

    
Creates a URI with the given query
    public static URI createURIWithQuery(URI uriString querythrows URISyntaxException {
        return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(),
                       queryuri.getFragment());
    }
    public static CompositeData parseComposite(URI urithrows URISyntaxException {
        CompositeData rc = new CompositeData();
        rc.scheme = uri.getScheme();
        String ssp = stripPrefix(uri.getSchemeSpecificPart().trim(), "//").trim();
        parseComposite(urircssp);
        rc.fragment = uri.getFragment();
        return rc;
    }

    

Parameters:
uri
rc
ssp
Throws:
java.net.URISyntaxException
    private static void parseComposite(URI uriCompositeData rcString sspthrows URISyntaxException {
        String componentString;
        String params;
        if (!checkParenthesis(ssp)) {
            throw new URISyntaxException(uri.toString(), "Not a matching number of '(' and ')' parenthesis");
        }
        int p;
        int intialParen = ssp.indexOf("(");
        if (intialParen == 0) {
            rc.host = ssp.substring(0, intialParen);
            p = rc.host.indexOf("/");
            if (p >= 0) {
                rc.path = rc.host.substring(p);
                rc.host = rc.host.substring(0, p);
            }
            p = ssp.lastIndexOf(")");
            componentString = ssp.substring(intialParen + 1, p);
            params = ssp.substring(p + 1).trim();
        } else {
            componentString = ssp;
            params = "";
        }
        String components[] = splitComponents(componentString);
        rc.components = new URI[components.length];
        for (int i = 0; i < components.lengthi++) {
            rc.components[i] = new URI(components[i].trim());
        }
        p = params.indexOf("?");
        if (p >= 0) {
            if (p > 0) {
                rc.path = stripPrefix(params.substring(0, p), "/");
            }
            rc.parameters = parseQuery(params.substring(p + 1));
        } else {
            if (params.length() > 0) {
                rc.path = stripPrefix(params"/");
            }
            rc.parameters = emptyMap();
        }
    }

    

Parameters:
str
Returns:
    private static String[] splitComponents(String str) {
        List<Stringl = new ArrayList<String>();
        int last = 0;
        int depth = 0;
        char chars[] = str.toCharArray();
        for (int i = 0; i < chars.lengthi++) {
            switch (chars[i]) {
            case '(':
                depth++;
                break;
            case ')':
                depth--;
                break;
            case ',':
                if (depth == 0) {
                    String s = str.substring(lasti);
                    l.add(s);
                    last = i + 1;
                }
                break;
            default:
            }
        }
        String s = str.substring(last);
        if (s.length() != 0) {
            l.add(s);
        }
        String rc[] = new String[l.size()];
        l.toArray(rc);
        return rc;
    }
    public static String stripPrefix(String valueString prefix) {
        if (value.startsWith(prefix)) {
            return value.substring(prefix.length());
        }
        return value;
    }
    public static URI stripScheme(URI urithrows URISyntaxException {
        return new URI(stripPrefix(uri.getSchemeSpecificPart().trim(), "//"));
    }
    public static String createQueryString(Map<String,Stringoptionsthrows URISyntaxException {
        try {
            if (options.size() > 0) {
                StringBuffer rc = new StringBuffer();
                boolean first = true;
                for (Map.Entry<String,Stringentry : options.entrySet()) {
                    if (first) {
                        first = false;
                    } else {
                        rc.append("&");
                    }
                    String key = entry.getKey();
                    String value = entry.getValue();
                    rc.append(URLEncoder.encode(key"UTF-8"));
                    rc.append("=");
                    rc.append(URLEncoder.encode(value"UTF-8"));
                }
                return rc.toString();
            } else {
                return "";
            }
        } catch (UnsupportedEncodingException e) {
            throw (URISyntaxException)new URISyntaxException(e.toString(), "Invalid encoding").initCause(e);
        }
    }

    
Creates a URI from the original URI and the remaining paramaters

    public static URI createRemainingURI(URI originalURIMap paramsthrows URISyntaxException {
        String s = createQueryString(params);
        if (s.length() == 0) {
            s = null;
        }
        return createURIWithQuery(originalURIs);
    }
    public static URI changeScheme(URI bindAddrString schemethrows URISyntaxException {
        return new URI(schemebindAddr.getUserInfo(), bindAddr.getHost(), bindAddr.getPort(), bindAddr
            .getPath(), bindAddr.getQuery(), bindAddr.getFragment());
    }
    public static boolean checkParenthesis(String str) {
        boolean result = true;
        if (str != null) {
            int open = 0;
            int closed = 0;
            int i = 0;
            while ((i = str.indexOf('('i)) >= 0) {
                i++;
                open++;
            }
            i = 0;
            while ((i = str.indexOf(')'i)) >= 0) {
                i++;
                closed++;
            }
            result = open == closed;
        }
        return result;
    }
    public int indexOfParenthesisMatch(String str) {
        int result = -1;
        return result;
    }
New to GrepCode? Check out our FAQ X