Java URL Query Parse parseInstructions(final String query)

Here you can find the source of parseInstructions(final String query)

Description

Parses bnd instructions out of an url query string.

License

Apache License

Parameter

Parameter Description
query query part of an url.

Exception

Parameter Description

Return

parsed instructions as properties

Declaration

public static Properties parseInstructions(final String query)
        throws MalformedURLException 

Method Source Code

//package com.java2s;
/**/*from   w ww .jav a2  s. c  o  m*/
 * Copyright (C) FuseSource, Inc.
 * http://fusesource.com
 *
 * Licensed 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.
 */

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;

import java.net.URLDecoder;

import java.util.Properties;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    /**
     * Regex pattern for matching instructions when specified in url.
     */
    private static final Pattern INSTRUCTIONS_PATTERN = Pattern
            .compile("([a-zA-Z_0-9-]+)=([-!\"'()*+,.0-9A-Z_a-z%;:=/]+)");

    /**
     * Parses bnd instructions out of an url query string.
     *
     * @param query query part of an url.
     *
     * @return parsed instructions as properties
     *
     * @throws java.net.MalformedURLException if provided path does not comply to syntax.
     */
    public static Properties parseInstructions(final String query)
            throws MalformedURLException {
        final Properties instructions = new Properties();
        if (query != null) {
            try {
                // just ignore for the moment and try out if we have valid properties separated by "&"
                final String segments[] = query.split("&");
                for (String segment : segments) {
                    // do not parse empty strings
                    if (segment.trim().length() > 0) {
                        final Matcher matcher = INSTRUCTIONS_PATTERN
                                .matcher(segment);
                        if (matcher.matches()) {
                            instructions.setProperty(matcher.group(1),
                                    URLDecoder.decode(matcher.group(2),
                                            "UTF-8"));
                        } else {
                            throw new MalformedURLException(
                                    "Invalid syntax for instruction ["
                                            + segment
                                            + "]. Take a look at http://www.aqute.biz/Code/Bnd.");
                        }
                    }
                }
            } catch (UnsupportedEncodingException e) {
                // thrown by URLDecoder but it should never happen
                throwAsMalformedURLException(
                        "Could not retrieve the instructions from ["
                                + query + "]", e);
            }
        }
        return instructions;
    }

    /**
     * Creates an MalformedURLException with a message and a cause.
     *
     * @param message exception message
     * @param cause   exception cause
     *
     * @throws MalformedURLException the created MalformedURLException
     */
    private static void throwAsMalformedURLException(final String message,
            final Exception cause) throws MalformedURLException {
        final MalformedURLException exception = new MalformedURLException(
                message);
        exception.initCause(cause);
        throw exception;
    }
}

Related

  1. parse(String query, String encoding)
  2. parseParameters( Optional queryString)
  3. parseParameters(String query)
  4. parseQuery(String query)
  5. parseQuery(String query)