Example usage for java.lang Character Character

List of usage examples for java.lang Character Character

Introduction

In this page you can find the example usage for java.lang Character Character.

Prototype

@Deprecated(since = "9")
public Character(char value) 

Source Link

Document

Constructs a newly allocated Character object that represents the specified char value.

Usage

From source file:org.jfastcgi.client.FastCGIHandler.java

private int parseHeaders(final ResponseAdapter res, final InputStream is) throws IOException {
    String key = "";
    String value = "";

    int ch = is.read();

    if (ch < 0) {
        getLog().error("Can't contact FastCGI");
        res.sendError(HTTP_ERROR_BAD_GATEWAY);
        return -1;
    }//from   ww  w.  j a  v a2s  . com

    while (ch >= 0) {

        for (key = ""; ch >= 0 && ch != ' ' && ch != '\r' && ch != '\n' && ch != ':'; ch = is.read()) {
            key += new Character((char) ch).toString();
        }

        while (ch >= 0 && ch == ' ' || ch == ':') {
            ch = is.read();
        }

        for (value = ""; ch >= 0 && ch != '\r' && ch != '\n'; ch = is.read()) {
            value += new Character((char) ch).toString();
        }

        if (ch == '\r') {
            ch = is.read();
            if (ch == '\n') {
                ch = is.read();
            }
        }

        if (key.length() == 0) {
            return ch;
        }

        if (getLog().isDebugEnabled()) {
            getLog().debug("fastcgi:" + key + ": " + value);
        }

        if (key.equalsIgnoreCase("status")) {
            int status = 0;
            final int len = value.length();

            for (int i = 0; i < len; i++) {
                final char digit = value.charAt(i);

                if ('0' <= digit && digit <= '9') {
                    status = 10 * status + digit - '0';
                }

                else {
                    break;

                }
            }

            res.setStatus(status);
        } else if (key.equalsIgnoreCase("location")) {
            res.sendRedirect(value.toString());
        } else {
            res.addHeader(key.toString(), value.toString());
        }
    }

    return ch;
}

From source file:com.gdo.stencils.interpreted.SlotDescriptor.java

/**
 * Creates a slot from factory class.// ww  w .j  av  a 2  s  .  c  o  m
 */
private _Slot<C, S> createSlotFromFactory(C stclContext, int completionLevel, S container) {
    if (getLog().isTraceEnabled()) {
        String msg = String.format("Creating slot %s in %s from factory %s", this, container, getFactory());
        getLog().trace(stclContext, msg);
    }

    try {
        Class<_Slot<C, S>> cl = ClassHelper.loadClass(getFactory());
        if (cl == null) {
            if (getLog().isWarnEnabled()) {
                String msg = String.format("Cannot find slot class %s for slot %s in %s", getFactory(),
                        getName(), container);
                getLog().warn(stclContext, msg);
            }
            return null;
        }
        Object[] params = new Object[] { stclContext, container, getName(), new Character(getArity()),
                new Boolean(isTransient()) };
        _Slot<C, S> slot = ClassHelper.newInstance(cl, params);
        if (slot != null) {
            slot.setDescriptor(this);
            slot.setCompletionLevel(completionLevel);
        }
        return slot;
    } catch (Exception e) {
        logWarn(stclContext, "Cannot create slot instance %s for slot %s in %s (%s)", getFactory(), getName(),
                container, e);
        return null;
    }
}

From source file:eionet.gdem.utils.Utils.java

/**
 * Set XML character escapes./* ww  w . ja  v a2  s .  co  m*/
 */
private static void setXmlEscapes() {
    xmlEscapes = new HashMap<Character, String>();
    xmlEscapes.put(new Character('&'), "&amp;");
    xmlEscapes.put(new Character('<'), "&lt;");
    xmlEscapes.put(new Character('>'), "&gt;");
    xmlEscapes.put(new Character('"'), "&quot;");
    xmlEscapes.put(new Character('\''), "&apos;");
    xmlEscapes.put('\u001A', "?");
}

From source file:org.eclipse.internal.xtend.type.baseimpl.types.StringTypeImpl.java

@Override
public Object convert(final Object src, final Class targetType) {
    final String s = toString(src);
    if (targetType.isAssignableFrom(String.class))
        return s;
    else if (targetType.isAssignableFrom(Character.class) || targetType.isAssignableFrom(Character.TYPE)) {
        if (s.length() == 1)
            return new Character(s.charAt(0));
    } else if (targetType.isAssignableFrom(StringBuffer.class))
        return new StringBuffer(s);
    return super.convert(src, targetType);
}

From source file:com.tasktop.internal.hp.qc.core.model.comments.mylyn3_8.HtmlStreamTokenizer_m3_8.java

/**
 * Parses HTML character and entity references and returns the corresponding character.
 *//*from  ww  w .jav  a  2 s.c  om*/
private static Character parseReference(String s) {
    if (s.length() == 0) {
        return null;
    }

    if (s.charAt(0) == '#') {
        // character reference
        if (s.length() == 1) {
            return null;
        }

        try {
            int value;
            if (s.charAt(1) == 'x') {
                // Hex reference
                value = Integer.parseInt(s.substring(2), 16);
            } else {
                // Decimal reference
                value = Integer.parseInt(s.substring(1));
            }
            return new Character((char) value);
        } catch (NumberFormatException e) {
            return null;
        }
    } else {
        return entities.get(s);
    }
}

From source file:net.sf.ezmorph.bean.BeanMorpherTest.java

public void testMorph_PrimitiveBean_to_ObjectBean() {
    PrimitiveBean primitiveBean = new PrimitiveBean();
    primitiveBean.setPclass(Object.class);
    primitiveBean.setPstring("MORPH");
    morpherRegistry.registerMorpher(new BeanMorpher(ObjectBean.class, morpherRegistry));
    ObjectBean objectBean = (ObjectBean) morpherRegistry.morph(ObjectBean.class, primitiveBean);
    assertNotNull(objectBean);/*from  w ww  . j  a  v  a2s .co m*/
    assertEquals(Boolean.FALSE, objectBean.getPboolean());
    assertEquals(Byte.valueOf("0"), objectBean.getPbyte());
    assertEquals(Short.valueOf("0"), objectBean.getPshort());
    assertEquals(Integer.valueOf("0"), objectBean.getPint());
    assertEquals(Long.valueOf("0"), objectBean.getPlong());
    assertEquals(Float.valueOf("0"), objectBean.getPfloat());
    assertEquals(Double.valueOf("0"), objectBean.getPdouble());
    assertEquals(new Character('\0'), objectBean.getPchar());
    assertEquals(null, objectBean.getParray());
    assertEquals(null, objectBean.getPlist());
    assertEquals(null, objectBean.getPbean());
    assertEquals(null, objectBean.getPmap());
    assertEquals("MORPH", objectBean.getPstring());
    assertEquals(Object.class, objectBean.getPclass());
}

From source file:com.sjsu.crawler.util.RandomStringUtilsTest.java

/**
 * Checks if the string got by {@link RandomStringUtils#random(int)}
 * can be converted to UTF-8 and back without loss.
 *
 * @see <a href="http://issues.apache.org/jira/browse/LANG-100">LANG-100</a>
 *//*from w  ww  .  ja v a  2  s .c o m*/
@Test
public void testLang100() {
    final int size = 5000;
    final Charset charset = Charset.forName("UTF-8");
    final String orig = RandomStringUtils.random(size);
    final byte[] bytes = orig.getBytes(charset);
    final String copy = new String(bytes, charset);

    // for a verbose compare:
    for (int i = 0; i < orig.length() && i < copy.length(); i++) {
        final char o = orig.charAt(i);
        final char c = copy.charAt(i);
        assertEquals("differs at " + i + "(" + Integer.toHexString(new Character(o).hashCode()) + ","
                + Integer.toHexString(new Character(c).hashCode()) + ")", o, c);
    }
    // compare length also
    assertEquals(orig.length(), copy.length());
    // just to be complete
    assertEquals(orig, copy);
}

From source file:org.protorabbit.model.impl.DefaultEngine.java

private static IParameter[] getParams(String paramsString) {

    paramsString = paramsString.trim();/* w  ww  .  j  a v  a  2  s.  c o m*/
    // for simple case of one item
    if (paramsString.indexOf(",") == -1 && paramsString.startsWith("\"") && paramsString.endsWith("\"")) {
        String text = paramsString.substring(0, paramsString.length());
        IParameter param = getParameter(text, new Character('\"'));
        IParameter[] params = new IParameter[1];
        params[0] = param;
        return params;
    }

    // otherwise we have quoted text or JSON objects
    ArrayList<IParameter> params = new ArrayList<IParameter>();
    int index = 0;
    int paramStart = index;
    boolean inQuote = false;
    boolean inArray = false;
    boolean inObject = false;
    int braceDepth = 0;
    int arrayDepth = 0;
    Character lastToken = null;
    while (index < paramsString.length()) {
        char c = paramsString.charAt(index);
        switch (c) {
        // string
        case '\"': {
            if (inQuote) {
                // if not escaped end the quote
                if (index > 0 && paramsString.charAt(index - 1) != '\\') {
                    inQuote = false;
                    lastToken = new Character('\"');
                }
            } else if (!inArray && !inObject) {
                inQuote = true;
                lastToken = new Character('\"');
            }
            break;
        }
        case '[': {
            if (!inQuote) {
                arrayDepth += 1;
                lastToken = new Character('[');
                inArray = true;
            }
            break;
        }
        case ']': {
            if (!inQuote) {
                arrayDepth -= 1;
                if (arrayDepth == 0 && braceDepth == 0) {
                    inArray = false;
                    String text = paramsString.substring(paramStart, index + 1).trim();
                    IParameter param = getParameter(text, lastToken);
                    params.add(param);
                    paramStart = index + 1;
                    lastToken = new Character(']');
                }
            }
            break;
        }
        case '{': {
            if (!inQuote) {
                if (braceDepth == 0) {
                    inObject = true;
                    paramStart = index;
                    lastToken = new Character('{');
                }
                braceDepth += 1;
            }
            break;
        }
        case '}': {
            if (!inQuote) {
                braceDepth -= 1;
                lastToken = new Character('}');
            }
            if (braceDepth == 0 && arrayDepth == 0 && lastToken != null && lastToken.charValue() != ']'
                    && lastToken.charValue() != '}') {
                inObject = false;
                String text = paramsString.substring(paramStart, index + 1).trim();
                IParameter param = getParameter(text, lastToken);
                params.add(param);
                paramStart = index + 1;
                lastToken = null;
            }
            break;
        }
        // could be a number or boolean
        case ',': {
            if (!inQuote && !inArray && !inObject) {
                String text = paramsString.substring(paramStart, index).trim();
                IParameter param = getParameter(text, lastToken);
                params.add(param);
                paramStart = index + 1;
                lastToken = null;
            }
            break;
        }
        }
        index += 1;
    }
    // get the trailing param if there is one
    if (paramStart < paramsString.length()) {
        String text = paramsString.substring(paramStart, paramsString.length());
        IParameter param = getParameter(text, lastToken);
        params.add(param);
    }
    IParameter[] a = new IParameter[params.size()];
    return params.toArray(a);
}

From source file:org.novoj.utils.datePattern.DatePatternConverter.java

/**
 * This method decomposes original pattern into the recognizable chunks. Chunks are composed of:
 * - same letters/*from   w w  w  .  ja v a  2 s. co  m*/
 * - parts escaped with apostrophe (anything could be inside)
 * - unknown characters whose are not letters (comma, dot, double dot, space and so on)
 *
 * @param pattern
 * @return
 */
@SuppressWarnings({ "OverlyLongMethod", "ImplicitNumericConversion" })
public List<String> getPatternDecomposition(String pattern) {
    List<String> patternDecomposition = new ArrayList<String>();
    boolean escaped = false;
    char lastLetter = (char) 0;
    StringBuilder buffer = new StringBuilder();
    for (int i = 0; i < pattern.length(); i++) {
        char letter = pattern.charAt(i);
        if (letter == '\'') {
            if (escaped) {
                if (pattern.charAt(i + 1) == '\'') {
                    i++;
                    buffer.append("\'\'");
                } else {
                    escaped = false;
                    buffer.append('\'');
                    buffer = resetBuffer(patternDecomposition, buffer);
                }
            } else {
                buffer = resetBuffer(patternDecomposition, buffer);
                escaped = true;
                buffer.append('\'');
            }
        } else if (escaped) {
            buffer.append(letter);
        } else if (Character.isLetter(letter)) {
            if (letter == lastLetter || lastLetter == (char) 0) {
                buffer.append(letter);
            } else {
                buffer = resetBuffer(patternDecomposition, buffer);
                buffer.append(letter);
            }
        } else if (specialCharacters.contains(new Character(letter))) {
            buffer = resetBuffer(patternDecomposition, buffer);
            patternDecomposition.add(String.valueOf(letter));
        } else {
            if (buffer.length() > 0 && Character.isLetter(buffer.charAt(0))) {
                buffer = resetBuffer(patternDecomposition, buffer);
            }
            buffer.append(letter);
        }
        lastLetter = letter;
    }
    resetBuffer(patternDecomposition, buffer);
    return patternDecomposition;
}

From source file:org.floggy.synchronization.jme.core.impl.JSONSerializationManagerTest.java

/**
* DOCUMENT ME!/*w w  w  . j  a  v  a  2  s  .c o  m*/
*
* @throws Exception DOCUMENT ME!
*/
public void testSendCharNotNull() throws Exception {
    JSONStringer stringer = new JSONStringer();
    String name = "firstName";
    Character value = new Character('F');

    stringer.object();
    JSONSerializationManager.send(name, value, stringer);
    stringer.endObject();

    String expected = "{\"" + name + "\":\"" + value + "\"}";

    assertEquals(expected, stringer.toString());
}