Java Encode encodedStringToProperties(String theEncodedPropertyString)

Here you can find the source of encodedStringToProperties(String theEncodedPropertyString)

Description

encoded String To Properties

License

Open Source License

Parameter

Parameter Description
theEncodedPropertyString a parameter

Return

the properties

Declaration

public static Properties encodedStringToProperties(String theEncodedPropertyString) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. 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://from   w  w w. jav  a 2s .  com
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/

import java.io.ByteArrayInputStream;

import java.io.IOException;

import java.util.Properties;

public class Main {
    /**
     * @param theEncodedPropertyString
     * @return the properties
     */
    public static Properties encodedStringToProperties(String theEncodedPropertyString) {
        try {
            return getPropertiesFromEncodedString(theEncodedPropertyString);
        } catch (IOException ee) {
            return null;
        }
    }

    /**
     * @param theEncodedPropertyString
     * @return the properties
     * @throws IOException
     */
    public static Properties getPropertiesFromEncodedString(String theEncodedPropertyString) throws IOException {
        if (theEncodedPropertyString == null)
            return null;
        return (getPropertiesFromString(decodeName(theEncodedPropertyString)));
    }

    /**
     * Methods for creating Properties objects from strings.
     * 
     * Then "Encoded" versions are used on values that are stored into a
     * properties file (think of them as sub-properties). They do the encoding
     * necessary to turn a properties object into a string that has legal
     * "value" syntax (they actually do more than they need to, but its all
     * non-destructive).
     * @param thePropertyString 
     * @return the properties from the string
     * @throws IOException 
     */
    public static Properties getPropertiesFromString(String thePropertyString) throws IOException {
        if (thePropertyString == null)
            return null;
        ByteArrayInputStream in = new ByteArrayInputStream(thePropertyString.getBytes());

        Properties props = new Properties();
        props.load(in); // throws IOException
        in = null;
        return props;
    }

    /**
     * @param theName
     * @return the decoded name
     */
    public static String decodeName(String theName) {
        int theSize = theName.length();
        int kk;
        StringBuffer decoded = new StringBuffer(theSize);
        char ch;

        for (int ii = 0; ii < theSize; ii++) {
            ch = theName.charAt(ii);
            if (ch == '%') {
                ch = theName.charAt(++ii);
                kk = Character.digit(ch, 16);
                kk *= 16;
                ch = theName.charAt(++ii);
                kk += Character.digit(ch, 16);
                decoded.append((char) kk);
            } else {
                decoded.append(ch);
            }
        }

        return decoded.toString();
    }
}

Related

  1. encodeAsModifiedUTF8(String str)
  2. encodeAttribute(String value)
  3. encodeBackslashAhead(char c, char next, Appendable buffer)
  4. encodeCodePoolData(InputStream input)
  5. encodedInputStreamReader(InputStream stream, String encoding)
  6. encodeEmail(String s)
  7. encodeFile(String filePath)
  8. encodeForFilter(final String filterString)
  9. encodeForString(String str)