Monday, June 18, 2012

Changing terminal prompt @z shell

http://www.nparikh.org/unix/prompt.php


For Z Shell

PROMPT=$'[%n@%m/%c]$'

This changes the terminal prompt as
user@hostname/current_directory_name

Monday, May 28, 2012

Boundary Matchers

Until now, we've only been interested in whether or not a match is found at some location within a particular input string. We never cared about where in the string the match was taking place.

You can make your pattern matches more precise by specifying such information with boundary matchers. For example, maybe you're interested in finding a particular word, but only if it appears at the beginning or end of a line. Or maybe you want to know if the match is taking place on a word boundary, or at the end of the previous match.

The following table lists and explains all the boundary matchers.
Boundary Construct     Description
^     The beginning of a line
$     The end of a line
\b     A word boundary
\B     A non-word boundary
\A     The beginning of the input
\G     The end of the previous match
\Z     The end of the input but for the final terminator, if any
\z     The end of the input

The following examples demonstrate the use of boundary matchers ^ and $. As noted above, ^ matches the beginning of a line, and $ matches the end.


Enter your regex: ^dog$
Enter input string to search: dog
I found the text "dog" starting at index 0 and ending at index 3.

Enter your regex: ^dog$
Enter input string to search:       dog
No match found.

Enter your regex: \s*dog$
Enter input string to search:             dog
I found the text "            dog" starting at index 0 and ending at index 15.

Enter your regex: ^dog\w*
Enter input string to search: dogblahblah
I found the text "dogblahblah" starting at index 0 and ending at index 11.

The first example is successful because the pattern occupies the entire input string. The second example fails because the input string contains extra whitespace at the beginning. The third example specifies an expression that allows for unlimited white space, followed by "dog" on the end of the line. The fourth example requires "dog" to be present at the beginning of a line followed by an unlimited number of word characters.

To check if a pattern begins and ends on a word boundary (as opposed to a substring within a longer string), just use \b on either side; for example, \bdog\b



Enter your regex: \bdog\b
Enter input string to search: The dog plays in the yard.
I found the text "dog" starting at index 4 and ending at index 7.

Enter your regex: \bdog\b
Enter input string to search: The doggie plays in the yard.
No match found.

To match the expression on a non-word boundary, use \B instead:


Enter your regex: \bdog\B
Enter input string to search: The dog plays in the yard.
No match found.

Enter your regex: \bdog\B
Enter input string to search: The doggie plays in the yard.
I found the text "dog" starting at index 4 and ending at index 7.

To require the match to occur only at the end of the previous match, use \G:


Enter your regex: dog
Enter input string to search: dog dog
I found the text "dog" starting at index 0 and ending at index 3.
I found the text "dog" starting at index 4 and ending at index 7.

Enter your regex: \Gdog
Enter input string to search: dog dog
I found the text "dog" starting at index 0 and ending at index 3.

Here the second example finds only one match, because the second occurrence of "dog" does not start at the end of the previous match.
Differences Among Greedy, Reluctant, and Possessive Quantifiers

There are subtle differences among greedy, reluctant, and possessive quantifiers.

Greedy quantifiers are considered "greedy" because they force the matcher to read in, or eat, the entire input string prior to attempting the first match. If the first match attempt (the entire input string) fails, the matcher backs off the input string by one character and tries again, repeating the process until a match is found or there are no more characters left to back off from. Depending on the quantifier used in the expression, the last thing it will try matching against is 1 or 0 characters.

The reluctant quantifiers, however, take the opposite approach: They start at the beginning of the input string, then reluctantly eat one character at a time looking for a match. The last thing they try is the entire input string.

Finally, the possessive quantifiers always eat the entire input string, trying once (and only once) for a match. Unlike the greedy quantifiers, possessive quantifiers never back off, even if doing so would allow the overall match to succeed.

To illustrate, consider the input string xfooxxxxxxfoo.


Enter your regex: .*foo  // greedy quantifier
Enter input string to search: xfooxxxxxxfoo
I found the text "xfooxxxxxxfoo" starting at index 0 and ending at index 13.

Enter your regex: .*?foo  // reluctant quantifier
Enter input string to search: xfooxxxxxxfoo
I found the text "xfoo" starting at index 0 and ending at index 4.
I found the text "xxxxxxfoo" starting at index 4 and ending at index 13.

Enter your regex: .*+foo // possessive quantifier
Enter input string to search: xfooxxxxxxfoo
No match found.

The first example uses the greedy quantifier .* to find "anything", zero or more times, followed by the letters "f" "o" "o". Because the quantifier is greedy, the .* portion of the expression first eats the entire input string. At this point, the overall expression cannot succeed, because the last three letters ("f" "o" "o") have already been consumed. So the matcher slowly backs off one letter at a time until the rightmost occurrence of "foo" has been regurgitated, at which point the match succeeds and the search ends.

The second example, however, is reluctant, so it starts by first consuming "nothing". Because "foo" doesn't appear at the beginning of the string, it's forced to swallow the first letter (an "x"), which triggers the first match at 0 and 4. Our test harness continues the process until the input string is exhausted. It finds another match at 4 and 13.

The third example fails to find a match because the quantifier is possessive. In this case, the entire input string is consumed by .*+, leaving nothing left over to satisfy the "foo" at the end of the expression. Use a possessive quantifier for situations where you want to seize all of something without ever backing off; it will outperform the equivalent greedy quantifier in cases where the match is not immediately found.

Tuesday, February 21, 2012

Thursday, February 9, 2012

Http Client

There are several ways we might accomplish an HTTP file upload on Android. The way
that gives us the most flexibility is to involve importing and using libraries from Apache’s HTTP Components that were not fully included with Android.

MultipartEntity allows for the
making of multipart/form-data style posts to servers. This is the same mechanism used
by browsers to do uploads from forms that allow the user to select a file.

http://hc.apache.org/httpclient-3.x/tutorial.html
Or the latest one can be downloaded from the below link
http://hc.apache.org/downloads.cgi

Android Repackaging

Jar Jar Links is a utility that makes it easy to repackage Java libraries and embed them into your own distribution. This is useful for two reasons:

You can easily ship a single jar file with no external dependencies.

You can avoid problems where your library depends on a specific version of a library, which may conflict with the dependencies of another library.

How does it work?

Jar Jar Links includes an Ant task that extends the built-in jar task. The normal zipfileset element is used to embed jar files. A new rule element is added which uses wildcards patterns to rename the embedded class files. Bytecode transformation (via ASM) is used to change references to the renamed classes, and special handling is provided for moving resource files and transforming string literals.

http://code.google.com/p/jarjar/

Android phone Numbers

Google's common Java, C++ and Javascript library for parsing, formatting, storing and validating international phone numbers. The Java version is optimized for running on smartphones, and is used by the Android framework since 4.0 (Ice Cream Sandwich).

http://code.google.com/p/libphonenumber/

Sunday, February 5, 2012

Performance HTML

http://developer.yahoo.com/performance/rules.html#js_bottom

HTML 5 resources

http://www.html5rocks.com/en/mobile

HTML 5

This URL can be used to find all tags and features of HTML 5 supported by your browser.

findmebyip.com: This site will show you, for the browser you're using to load the page, support for HTML5 and CSS3 features in a great table format. It also includes some of the newest JavaScript features.

html5test.com: Another site that shows you HTML5 support for the browser you use to load the page. This site assigns points based on the amount of support your browser has so you can easily compare it with other browsers.

caniuse.com: This site is a great one for comparing feature support across multiple browsers. It uses an easy-to-read table format that shows support of both HTML5 and CSS3 features for current, near future and up to 3 versions back of all the major browsers on both desktop and mobile.

theexpressiveweb.com/: This Adobe site has examples of a variety of HTML5 and CSS3 features, resources, browser support, and even code snippets that show how to detect a feature and fallback if the feature is not supported.

Source - http://wickedlysmart.com/hfhtml5/browsersupport.html

Wednesday, January 18, 2012

Problem Classifier

problemclassifier.appspot.com

The above website can be used to find questions based on keywords.

Tuesday, January 10, 2012

Java Profiler

If you launch the Java virtual machine with the -Xprof flag, it runs a rudimentary profiler
that keeps track of the methods in your code that were executed most often. The
profiling information is sent to System.out. The output also tells you which methods
were compiled by the just-in-time compiler.

Objects on the heap

You can use the jmap utility to get a heap dump that shows you every object on the
heap. Use these commands:
jmap -dump:format=b,file=dumpFileName processID
jhat dumpFileName
Then, point your browser to localhost:7000. You will get a web application that lets
you drill down into the contents of the heap at the time of the dump.

JConsole

Java SE 5.0 added support for monitoring and management of Java applications,
allowing the installation of agents in the virtual machine that track memory consumption,
thread usage, class loading, and so on. This feature is particularly important
for large and long-running Java programs such as application servers. As a
demonstration of these capabilities, the JDK ships with a graphical tool called jconsole
that displays statistics about the performance of a virtual machine (see Figure
11–3). Find out the ID of the operating system process that runs the virtual machine.
In UNIX/Linux, run the ps utility; in Windows, use the task manager. Then launch
the jconsole program:
jconsole processID
The console gives you a wealth of information about your running program. See
http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html for more information.
Java SE 5.0 added the -Xlint option to the compiler for spotting common code problems.
For example, if you compile with the command
javac -Xlint:fallthrough
then the compiler reports missing break statements in switch statements. (The term
“lint” originally described a tool for locating potential problems in C programs, and
is now generically applied to tools that flag constructs that are questionable but not
illegal.)

The following options are available:

-Xlint or -Xlint:all Carries out all checks
-Xlint:deprecation Same as -deprecation, checks for deprecated methods
-Xlint:fallthrough Checks for missing break statements in switch statements
-Xlint:finally Warns about finally clauses that cannot complete normally
-Xlint:none Carries out none of the checks
-Xlint:path Checks that all directories on the class path and source
path exist
-Xlint:serial Warns about serializable classes without serialVersionUID
(see Chapter 1 of Volume II)
-Xlint:unchecked Warns of unsafe conversions between generic and raw
types (see Chapter 12)

Catching Uncaught Exceptions

Having stack traces of uncaught exceptions show up in System.err is not ideal. These
messages are confusing to end users if they happen to see them, and they are not
available for diagnostic purposes when you need them. A better approach is to log
them to a file. As of Java SE 5.0, you can change the handler for uncaught exceptions
with the static Thread.setDefaultUncaughtExceptionHandler method:
Thread.setDefaultUncaughtExceptionHandler(
new Thread.UncaughtExceptionHandler()
{
public void uncaughtException(Thread t, Throwable e)
{
save information in log file
};
});

System err and System out

It is often handy to trap program errors in a file. However, errors are sent to
System.err, not System.out. Therefore, you cannot simply trap them by running
java MyProgram > errors.txt
Instead, capture the error stream as
java MyProgram 2> errors.txt
To capture both System.err and System.out in the same file, use
java MyProgram >& errors.txt
This works in bash and the Windows shell.