Recent Notes
Displaying keyword search results 81 - 90
Created by alfa on May 25, 2011 20:56:16
Last update: May 25, 2011 20:57:31
The general construct of a non-capturing group is: (?:X) , i.e., add ?: after the opening bracket of an otherwise capturing group.
Example code:
import java.util.regex.*;
public class NonC...
Output:
Matched: a capturing
Subgroup 1: a
Subgroup ...
Created by alfa on May 25, 2011 15:22:08
Last update: May 25, 2011 15:25:05
Use Matcher.find() to find the next regex match until all matches are exhausted.
import java.util.regex.*;
public class RegE...
To get case insensitive match, use:
Pattern p = Pattern.compile("(?i)\\b(m|c)\\w*(n|r|...
Created by alfa on May 24, 2011 14:51:29
Last update: May 24, 2011 14:51:29
The following code generates warning "warning: [unchecked] unchecked call to getMethod" when compiled:
Class c = Class.forName("MyClass");
Method m = ...
This is because the method getMethod is parameterized , while the variable c is not parameterized.
There are two ways to eliminate this warning:
Make c parameterized:
Class<?> c = Class.forName("MyClass");
Meth...
Suppress the warning with annotation:
Class c = Class.forName("MyClass");
@SuppressWa...
Created by freyo on May 24, 2011 09:15:14
Last update: May 24, 2011 09:15:14
Java built-in X.509 certificate factory reads CertPath objects encoded in PkiPath or PKCS7 formats. The META-INF/<key_alias>.RSA file generated by Java jarsigner is in PKCS7 format.
Example code:
import java.util.*;
import java.io.*;
import...
PKCS7 files can also be generated by openssl from certificate file(s):
openssl crl2pkcs7 -nocrl -certfile Certs.pem -out ...
Convert PKCS7 from DER to PEM:
openssl pkcs7 -in certs.pk7 -inform DER -out certs...
Sample PKCS7 file:
-----BEGIN PKCS7-----
MIIERwYJKoZIhvcNAQcCoIIEO...
Created by freyo on May 23, 2011 14:30:18
Last update: May 23, 2011 14:31:08
There are two distinct ways to process XPath: with namespace and without namespace. The code is different depending on whether the parser is namespace aware.
Code without namespace:
import java.io.*;
import javax.xml.parsers.*;
...
code with namespace:
import java.io.*;
import java.util.Iterator;
...
XML without namespace:
<?xml version="1.0" encoding="UTF-8" standalone="n...
XML with namespace:
<?xml version="1.0" encoding="UTF-8" standalone="n...
The same XPath expression works for both XML files when the parser is not namespace aware.
When the parser is namespace aware, you have to adjust the XPath accordingly depending on whether the XML has namespace declarations: " /test-license/licensee/name/text() " works for the XML file without namespace, while " /p:test-license/p:licensee/p:name/text() " works for the XML file with namespace.
Created by freyo on May 20, 2011 09:25:20
Last update: May 23, 2011 12:11:42
The javax.xml.crypto and javax.xml.crypto.dsig packages are not available in Android (as of version 2.3). Therefore, standard Java API does not work. But you can use the Apache Santuario library to do that. Here are the steps:
Download the xml security source distribution (curently version 1.4.4).
Build with ant.
Create your own library jar (only the apache classes, no javax):
jar -cf xmlsec-1.4.4.jar -C build/classes org
Copy xmlsec-1.4.4.jar to the libs directory of your Android project.
Here's the Java code:
import java.io.*;
import javax.xml.parsers.*;
...
Created by freyo on May 19, 2011 08:02:04
Last update: May 19, 2011 08:02:55
http://android.git.kernel.org/?p=platform/libcore.git;a=tree;f=luni/src/main/java/org/apache/harmony/xml;hb=HEAD
Created by freyo on May 17, 2011 11:13:17
Last update: May 17, 2011 11:13:17
This is an odd-ball content provider in that it doesn't provide database records, but provides a resource as a stream. It can be used to provide media files or XML resources. Start the project with:
tools/android create project --package com.android... Create assets directory and add an XML file ( assets/demo.xml ): <? xml version="1.0" encoding="UTF-8"?> <people... Edit the layout ( res/layout/main.xml ): <?xml version="1.0" encoding="utf-8"?> <LinearL... Edit src/com/android/cptest/Dummy.java : package com.android.cptest; import java.io.... Add content provider ( src/com/android/cptest/XmlResource.java ): package com.android.cptest; import java.io.... Update AndroidManifest.xml : <?xml version="1.0" encoding="utf-8"?> <manifes... Add this section to the end of build.xml : <target name="-package-resources"> <ech... Build and install: ant install Screenshot: Remove the Dummy activity ( AndroidManifest.xml ): <?xml version="1.0" encoding="utf-8"?> <manifes... Create a new project for...
Created by freyo on May 17, 2011 08:46:50
Last update: May 17, 2011 08:47:35
When I used AssetManager to open an XML file in a ContentProvider :
getContext().getAssets().openFd("demo.xml");
it failed with this exception:
java.io.FileNotFoundException: This file can not b...
It turned out that openFd only works for asset files that are not compressed. It works on media files such as .mp3 , .jpg , .wmv etc., which are not compressed during packaging. XML files are compressed by dedault, causing openFd to fail.
An Android ticket was opened on this issue. Till that is fixed, the workaround is to add an exclusion in build.xml :
<?xml version="1.0" encoding="UTF-8"?>
<pro...
Created by jinx on May 16, 2011 20:49:44
Last update: May 16, 2011 20:57:04
You can't break out of an if block because it's not a loop. But what if you want to abort execution in the middle of an if block? There's a hack to wrap the if inside a while and then break out. Nice hack, but not absolutely necessary.
Example code:
<?php
$a = '1';
$more_work = rand(0, 1);
...