Monday, January 31, 2011

How to use regular Expressins in String manipulation

Regular expressions are power full and efficient. i used them recently for my project and here i will just show few usages of it

Its so simple

Pattern p = Pattern.compile(regexPatternString);
Matcher m = p.matcher(textString);

then evaluate using
if(m.matches()){
 //write what ever if that match you want to do

//pattern to get character contains only 1 to 3(1,2,3) and letter c( C and c)
Pattern p = Pattern.compile("[.1-3cC]");

//only Numerical values string that contains only digits and any number of digits
Pattern p = Pattern.compile(("[0-9].*")

//get string that only contains "sanjeewa" or "malalgoda"
Pattern p = Pattern.compile(("sanjeewa|malalgoda")

Wednesday, January 26, 2011

Get Tag value fro XML Document(org.w3c.dom.Document) -Blackberry Java development


    /**
     * @param document
     * @param node
     * @param index
     * @return element value
     * getTag(Document document, String node,int index)
     */
    public static String getTag(Document document, String node,int index) 
    {
    NodeList nodeList = document.getElementsByTagName(node);
    String returnString = "";
    if (nodeList != null && nodeList.getLength() > 0) {
        nodeList = nodeList.item(index).getChildNodes();
        if (nodeList != null && nodeList.getLength() > 0) {
        returnString = nodeList.item(0).getNodeValue();
        }
    }
    return returnString;
    }

How to Read RSA Encrypted File to Encoded Image -Java BlackBerry Develpment

 /**
     * @param path
     * @return encoded base64 string String encodeToBase64(String path)
     * @throws IOException
     * @throws CryptoUnsupportedOperationException 
     * @throws CryptoTokenException 
     * @throws CryptoException 
     */
    public static EncImage encodeToBase64(String path) throws IOException, CryptoException, CryptoTokenException, CryptoUnsupportedOperationException {
    EncImage encImage =  new EncImage();
    FileConnection fileConToRead = null;
    InputStream inputStream=null;


    try {
        fileConToRead = (FileConnection) Connector.open("file://" + path, Connector.READ);
        int size = (int) fileConToRead.fileSize();
        inputStream = fileConToRead.openInputStream();
        byte bytes[] = new byte[size];
        inputStream.read(bytes, 0, size);

        // If RSA enables
        if (Main.isRSAEncrypted) {
        CryptoRSA cryptoRSA = new CryptoRSA();
        bytes = cryptoRSA.decrypt(bytes);
        }
        
        EncodedImage image = EncodedImage.createEncodedImage(bytes, 0, bytes.length);
        encImage.setImageHeight(image.getHeight());
        encImage.setImageWidth(image.getWidth());

        byte encodedBytes[] = Base64.encodeBase64(bytes);
        encImage.setEncodesString(new String(encodedBytes));

    } finally {
        try {
        // try to close the FileConnection
        fileConToRead.close();
        inputStream.close();
        } catch (IOException e) {
        //finally connection assigns to null
        }
        // connection set to null
        fileConToRead = null;
        inputStream=null;

    }
    return encImage;
    }