FileDocCategorySizeDatePackage
VDataBuilder.javaAPI DocAndroid 1.5 API9623Wed May 06 22:41:56 BST 2009android.syncml.pim

VDataBuilder

public class VDataBuilder extends Object implements VBuilder
Store the parse result to custom datastruct: VNode, PropertyNode Maybe several vcard instance, so use vNodeList to store. VNode: standy by a vcard instance. PropertyNode: standy by a property line of a card.

Fields Summary
private static String
LOG_TAG
public List
vNodeList
type=VNode
private int
mNodeListPos
private VNode
mCurrentVNode
private PropertyNode
mCurrentPropNode
private String
mCurrentParamType
private String
mCharset
Assumes that each String can be encoded into byte array using this encoding.
private boolean
mStrictLineBreakParsing
Constructors Summary
public VDataBuilder()

    
      
        mCharset = "ISO-8859-1";
        mStrictLineBreakParsing = false;
    
public VDataBuilder(String encoding, boolean strictLineBreakParsing)

        mCharset = encoding;
        mStrictLineBreakParsing = strictLineBreakParsing;
    
Methods Summary
private java.lang.StringencodeString(java.lang.String originalString, java.lang.String targetEncoding)

        Charset charset = Charset.forName(mCharset);
        ByteBuffer byteBuffer = charset.encode(originalString);
        // byteBuffer.array() "may" return byte array which is larger than
        // byteBuffer.remaining(). Here, we keep on the safe side.
        byte[] bytes = new byte[byteBuffer.remaining()];
        byteBuffer.get(bytes);
        try {
            return new String(bytes, targetEncoding);
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    
public voidend()

    
public voidendProperty()

        //  System.out.println("- endProperty. ");
    
public voidendRecord()

        VNode endNode = vNodeList.get(mNodeListPos);
        endNode.parseStatus = 0;
        while(mNodeListPos > 0){
            mNodeListPos--;
            if((vNodeList.get(mNodeListPos)).parseStatus == 1)
                break;
        }
        mCurrentVNode = vNodeList.get(mNodeListPos);
    
public java.lang.StringgetResult()

        return null;
    
private java.lang.StringlistToString(java.util.Collection list)

        StringBuilder typeListB = new StringBuilder();
        for (String type : list) {
            typeListB.append(type).append(";");
        }
        int len = typeListB.length();
        if (len > 0 && typeListB.charAt(len - 1) == ';") {
            return typeListB.substring(0, len - 1);
        }
        return typeListB.toString();
    
public voidpropertyGroup(java.lang.String group)

        mCurrentPropNode.propGroupSet.add(group);
    
public voidpropertyName(java.lang.String name)

        mCurrentPropNode = new PropertyNode();
        mCurrentPropNode.propName = name;
    
public voidpropertyParamType(java.lang.String type)

        mCurrentParamType = type;
    
public voidpropertyParamValue(java.lang.String value)

        if (mCurrentParamType == null ||
                mCurrentParamType.equalsIgnoreCase("TYPE")) {
            mCurrentPropNode.paramMap_TYPE.add(value);
        } else {
            mCurrentPropNode.paramMap.put(mCurrentParamType, value);
        }

        mCurrentParamType = null;
    
public voidpropertyValues(java.util.List values)

        ContentValues paramMap = mCurrentPropNode.paramMap;
        
        String charsetString = paramMap.getAsString("CHARSET"); 

        boolean setupParamValues = false;
        //decode value string to propValue_bytes
        if (paramMap.containsKey("ENCODING")) {
            String encoding = paramMap.getAsString("ENCODING"); 
            if (encoding.equalsIgnoreCase("BASE64") ||
                    encoding.equalsIgnoreCase("B")) {
                if (values.size() > 1) {
                    Log.e(LOG_TAG,
                            ("BASE64 encoding is used while " +
                             "there are multiple values (" + values.size()));
                }
                mCurrentPropNode.propValue_bytes =
                    Base64.decodeBase64(values.get(0).
                            replaceAll(" ","").replaceAll("\t","").
                            replaceAll("\r\n","").
                            getBytes());
            }

            if(encoding.equalsIgnoreCase("QUOTED-PRINTABLE")){
                // if CHARSET is defined, we translate each String into the Charset.
                List<String> tmpValues = new ArrayList<String>();
                Vector<byte[]> byteVector = new Vector<byte[]>();
                int size = 0;
                try{
                    for (String value : values) {                                    
                        String quotedPrintable = value
                        .replaceAll("= ", " ").replaceAll("=\t", "\t");
                        String[] lines;
                        if (mStrictLineBreakParsing) {
                            lines = quotedPrintable.split("\r\n");
                        } else {
                            lines = quotedPrintable
                            .replace("\r\n", "\n").replace("\r", "\n").split("\n");
                        }
                        StringBuilder builder = new StringBuilder();
                        for (String line : lines) {
                            if (line.endsWith("=")) {
                                line = line.substring(0, line.length() - 1);
                            }
                            builder.append(line);
                        }
                        byte[] bytes = QuotedPrintableCodec.decodeQuotedPrintable(
                                builder.toString().getBytes());
                        if (charsetString != null) {
                            try {
                                tmpValues.add(new String(bytes, charsetString));
                            } catch (UnsupportedEncodingException e) {
                                Log.e(LOG_TAG, "Failed to encode: charset=" + charsetString);
                                tmpValues.add(new String(bytes));
                            }
                        } else {
                            tmpValues.add(new String(bytes));
                        }
                        byteVector.add(bytes);
                        size += bytes.length;
                    }  // for (String value : values) {
                    mCurrentPropNode.propValue_vector = tmpValues;
                    mCurrentPropNode.propValue = listToString(tmpValues);

                    mCurrentPropNode.propValue_bytes = new byte[size];

                    {
                        byte[] tmpBytes = mCurrentPropNode.propValue_bytes;
                        int index = 0;
                        for (byte[] bytes : byteVector) {
                            int length = bytes.length;
                            for (int i = 0; i < length; i++, index++) {
                                tmpBytes[index] = bytes[i];
                            }
                        }
                    }
                    setupParamValues = true;
                } catch(Exception e) {
                    Log.e(LOG_TAG, "Failed to decode quoted-printable: " + e);
                }
            }  // QUOTED-PRINTABLE
        }  //  ENCODING
        
        if (!setupParamValues) {
            // if CHARSET is defined, we translate each String into the Charset.
            if (charsetString != null) {
                List<String> tmpValues = new ArrayList<String>();
                for (String value : values) {
                    String result = encodeString(value, charsetString);
                    if (result != null) {
                        tmpValues.add(result);
                    } else {
                        Log.e(LOG_TAG, "Failed to encode: charset=" + charsetString);
                        tmpValues.add(value);
                    }
                }
                values = tmpValues;
            }
            
            mCurrentPropNode.propValue_vector = values;
            mCurrentPropNode.propValue = listToString(values);
        }
        mCurrentVNode.propList.add(mCurrentPropNode);
    
public voidstart()

    
public voidstartProperty()

        //  System.out.println("+ startProperty. ");
    
public voidstartRecord(java.lang.String type)

        VNode vnode = new VNode();
        vnode.parseStatus = 1;
        vnode.VName = type;
        vNodeList.add(vnode);
        mNodeListPos = vNodeList.size()-1;
        mCurrentVNode = vNodeList.get(mNodeListPos);