Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.node;
  
  
Value node that contains a text value.
 
 public final class TextNode
     extends ValueNode
 {
     final static int INT_SPACE = ' ';
 
     final static TextNode EMPTY_STRING_NODE = new TextNode("");
 
     final String _value;
 
     public TextNode(String v) {  = v; }

    
Factory method that should be used to construct instances. For some common cases, can reuse canonical instances: currently this is the case for empty Strings, in future possible for others as well. If null is passed, will return null.

Returns:
Resulting TextNode object, if v is NOT null; null if it is.
 
     public static TextNode valueOf(String v)
     {
         if (v == null) {
             return null;
         }
         if (v.length() == 0) {
             return ;
         }
         return new TextNode(v);
     }
 
     @Override public JsonToken asToken() { return .; }

    
Yes indeed it is textual
 
     @Override
     public boolean isTextual() { return true; }
 
     @Override
     public String textValue() {
         return ;
     }

    
Method for accessing textual contents assuming they were base64 encoded; if so, they are decoded and resulting binary data is returned.
 
     public byte[] getBinaryValue(Base64Variant b64variant)
         throws IOException
     {
         ByteArrayBuilder builder = new ByteArrayBuilder(100);
         final String str = ;
         int ptr = 0;
         int len = str.length();
 
         main_loop:
         while (ptr < len) {
             // first, we'll skip preceding white space, if any
             char ch;
             do {
                 ch = str.charAt(ptr++);
                 if (ptr >= len) {
                     break main_loop;
                 }
             } while (ch <= );
             int bits = b64variant.decodeBase64Char(ch);
             if (bits < 0) {
                 _reportInvalidBase64(b64variantch, 0);
             }
             int decodedData = bits;
             // then second base64 char; can't get padding yet, nor ws
             if (ptr >= len) {
                 _reportBase64EOF();
             }
             ch = str.charAt(ptr++);
             bits = b64variant.decodeBase64Char(ch);
             if (bits < 0) {
                 _reportInvalidBase64(b64variantch, 1);
             }
             decodedData = (decodedData << 6) | bits;
             // third base64 char; can be padding, but not ws
             if (ptr >= len) {
                 // but as per [JACKSON-631] can be end-of-input, iff not using padding
                if (!b64variant.usesPadding()) {
                    // Got 12 bits, only need 8, need to shift
                    decodedData >>= 4;
                    builder.append(decodedData);
                    break;
                }
                _reportBase64EOF();
            }
            ch = str.charAt(ptr++);
            bits = b64variant.decodeBase64Char(ch);
            // First branch: can get padding (-> 1 byte)
            if (bits < 0) {
                if (bits != .) {
                    _reportInvalidBase64(b64variantch, 2);
                }
                // Ok, must get padding
                if (ptr >= len) {
                    _reportBase64EOF();
                }
                ch = str.charAt(ptr++);
                if (!b64variant.usesPaddingChar(ch)) {
                    _reportInvalidBase64(b64variantch, 3, "expected padding character '"+b64variant.getPaddingChar()+"'");
                }
                // Got 12 bits, only need 8, need to shift
                decodedData >>= 4;
                builder.append(decodedData);
                continue;
            }
            // Nope, 2 or 3 bytes
            decodedData = (decodedData << 6) | bits;
            // fourth and last base64 char; can be padding, but not ws
            if (ptr >= len) {
                // but as per [JACKSON-631] can be end-of-input, iff not using padding
                if (!b64variant.usesPadding()) {
                    decodedData >>= 2;
                    builder.appendTwoBytes(decodedData);
                    break;
                }
                _reportBase64EOF();
            }
            ch = str.charAt(ptr++);
            bits = b64variant.decodeBase64Char(ch);
            if (bits < 0) {
                if (bits != .) {
                    _reportInvalidBase64(b64variantch, 3);
                }
                decodedData >>= 2;
                builder.appendTwoBytes(decodedData);
            } else {
                // otherwise, our triple is now complete
                decodedData = (decodedData << 6) | bits;
                builder.appendThreeBytes(decodedData);
            }
        }
        return builder.toByteArray();
    }
    @Override
    public byte[] binaryValue() throws IOException
    {
        return getBinaryValue(Base64Variants.getDefaultVariant());
    }
    
    /* 
    /**********************************************************
    /* General type coercions
    /**********************************************************
     */
    @Override
    public String asText() {
        return ;
    }
    // note: neither fast nor elegant, but these work for now:
    @Override
    public boolean asBoolean(boolean defaultValue) {
        if ( != null) {
            if ("true".equals(.trim())) {
                return true;
            }
        }
        return defaultValue;
    }
    
    @Override
    public int asInt(int defaultValue) {
        return NumberInput.parseAsInt(defaultValue);
    }
    @Override
    public long asLong(long defaultValue) {
        return NumberInput.parseAsLong(defaultValue);
    }
    
    @Override
    public double asDouble(double defaultValue) {
        return NumberInput.parseAsDouble(defaultValue);
    }
    
    /* 
    /**********************************************************
    /* Serialization
    /**********************************************************
     */
    
    @Override
    public final void serialize(JsonGenerator jgSerializerProvider provider)
        throws IOExceptionJsonProcessingException
    {
        if ( == null) {
            jg.writeNull();
        } else {
            jg.writeString();
        }
    }
    /*
    /**********************************************************
    /* Overridden standard methods
    /**********************************************************
     */
    
    @Override
    public boolean equals(Object o)
    {
        if (o == thisreturn true;
        if (o == nullreturn false;
        if (o.getClass() != getClass()) { // final class, can do this
            return false;
        }
        return ((TextNodeo)..equals();
    }
    
    @Override
    public int hashCode() { return .hashCode(); }

    
Different from other values, Strings need quoting
    @Override
    public String toString()
    {
        int len = .length();
        len = len + 2 + (len >> 4);
        StringBuilder sb = new StringBuilder(len);
        appendQuoted(sb);
        return sb.toString();
    }
    protected static void appendQuoted(StringBuilder sbString content)
    {
        sb.append('"');
        CharTypes.appendQuoted(sbcontent);
        sb.append('"');
    }
    /*
    /**********************************************************
    /* Helper methods
    /**********************************************************
     */
    
    protected void _reportInvalidBase64(Base64Variant b64variantchar chint bindex)
        throws JsonParseException
    {
        _reportInvalidBase64(b64variantchbindexnull);
    }

    

Parameters:
bindex Relative index within base64 character unit; between 0 and 3 (as unit has exactly 4 characters)
    protected void _reportInvalidBase64(Base64Variant b64variantchar chint bindexString msg)
        throws JsonParseException
    {
        String base;
        if (ch <= ) {
            base = "Illegal white space character (code 0x"+Integer.toHexString(ch)+") as character #"+(bindex+1)+" of 4-char base64 unit: can only used between units";
        } else if (b64variant.usesPaddingChar(ch)) {
            base = "Unexpected padding character ('"+b64variant.getPaddingChar()+"') as character #"+(bindex+1)+" of 4-char base64 unit: padding only legal as 3rd or 4th character";
        } else if (!Character.isDefined(ch) || Character.isISOControl(ch)) {
            // Not sure if we can really get here... ? (most illegal xml chars are caught at lower level)
            base = "Illegal character (code 0x"+Integer.toHexString(ch)+") in base64 content";
        } else {
            base = "Illegal character '"+ch+"' (code 0x"+Integer.toHexString(ch)+") in base64 content";
        }
        if (msg != null) {
            base = base + ": " + msg;
        }
        throw new JsonParseException(base.);
    }
    protected void _reportBase64EOF()
        throws JsonParseException
    {
        throw new JsonParseException("Unexpected end-of-String when base64 content".);
    }
New to GrepCode? Check out our FAQ X