Showing posts with label tips. Show all posts
Showing posts with label tips. Show all posts

Sunday, March 2, 2008

SAX Parser tips

Recently I got couple of interesting questions from my friends who are working on XML and using SAX parser to 'parse' the XML data - for performance and memory efficient; SAX parser can work efficiently even for 2 GB XML files!

Identifying Self ending tags:
Actually in XML both <br/> and <br/></br> are equivalent. So, using SAX parser you can't find whether it is a self ending tag or not. However there is a work around for it - using locator objects!

For <br/>, in both startElement and endElement you get the same location (getLineNumber() and getColumn number()) will be same.

For <br/></br>, they will be different – column numbers will be different (or even line number!).

But, using Locator object with SAXParser might slightly decrease the performance.
Also one more thing, all SAX may not support Locators as this is an optional feature.

More about Locators can be found at http://www.saxproject.org/apidoc/org/xml/sax/Locator.html


Handling default attributes

Problem:
Input file : <xhtml:td>VI</xhtml:td>Benzyl</xhtml:td>

Output file :
<xhtml:td rowspan="1" colspan="1">VI</xhtml:td>
<xhtml:td align="left" rowspan="1" colspan="1">Benzyl</xhtml:td>

The data has "rowspan" , “colspan” automatically included in the output. But the same is not present in the input.

The dtd declaration for the xhtml:td is as below
<!ATTLIST %td.qname;
%attrs;
abbr %Text; #IMPLIED
axis CDATA #IMPLIED
headers IDREFS #IMPLIED
scope %Scope; #IMPLIED
xhtml:rowspan %Number; "1"
xhtml:colspan %Number; "1"
%cellhalign;
%cellvalign;
>

These attributes are coming because they have a default value in DTD.

In the DTD it is mentioned that the default value of the xhtml:rowspan is 1, so unless you specify some value the rowspan will be 1.

Even if you don’t declare that attribute, SAXParser automatically get the value from the DTD (a ‘special’ feature of SAX parser called DTD defaulting).

You can only handle this in SAX2 parser (not in SAX parser version 1.x). I think most of the SAX parser available (like one comes with JDK1.5) today are SAX2.

In your startElement method, you will get an object of Attributes2 instead of Attributes; Actually Attributes2 is a subclass of Attributes.

Attributes2 interface has method isSpecified() which returns true unless the attribute value was provided by DTD defaulting.

So, keep this check in startElement method:



public void startElement (String uri, String localName,
String qName, Attributes attributes) throws SAXException
{
if (attributes instanceof Attributes2) {
Attributes2 att = (Attributes2) attributes
for (int i = 0; i < att.getLength(); i++) {
if (att.isSpecified(i)) // present in xml file
System.out.println(att.getQName(i) + "=\"" + att.getValue(i) + "\"");
else {// not present in xml file, came from DTD.
}
}
} // if not, we don't have a choice output all attributes.
}



There is another better way to check whether the SAX Parser Attributes2 or not - by checking the system property http://xml.org/sax/features/use-attributes2
More details at http://www.saxproject.org/apidoc/org/xml/sax/package-summary.html#package_description

Sunday, February 17, 2008

Compare two word documents using MS Word

Compare two word documents using MS Word
1. Open a document.
2. On the Tools menu, click Compare and Merge Documents.
3. Select the document that you want to compare to the copy that is currently open.
4. Click the arrow next to Merge, and then do one of the following:
* To display the results of the comparison in the selected document, click Merge.
* To display the results in the document that is currently open, click Merge into current document. * To display the results in a new document, click Merge into new document.

The differences will be displayed as comments in the new document.





For example here the Last updated date has been changed from 13-04-07 to
11-05-07.

Firefox tips

To check Installed plugins

type: about:plugins in browser,

This will show up list of plugins that are installed like Flash player
plugin, Java plugin, Shockwave plugin.










Customize the display and location of the close tab button in the tab bar

You can customize the display and location of the close tab button in
the tab bar by using about:config to edit the preference
browser.tabs.closeButtons.

Values:
0 Display a close button on the active tab only
1 (Default) Display close buttons on all tabs
2 Don't display any close buttons
3 Display a single close button at the end of the tab bar (Firefox 1.xbehavior)

Speed up Firefox

1.Type "about:config" into the address bar and hit return. Scroll down and
look for the following entries:

network.http.pipelining network.http.proxy.pipelining
network.http.pipelining.maxrequests

Normally the browser will make one request to a web page at a time. When you
enable pipelining it will make several at once, which really speeds up page
loading.

2. Alter the entries as follows:

Set "network.http.pipelining" to "true"

Set "network.http.proxy.pipelining" to "true"

Set "network.http.pipelining.maxrequests" to some number like 30. This means
it will make 30 requests at once.

3. Lastly right-click anywhere and select New-> Integer. Name it "
nglayout.initialpaint.delay" and set its value to "0". This value is the
amount of time the browser waits before it acts on information it receives.

If you're using a broadband connection you'll load pages MUCH faster now!

Copyright (c) 2008 - Suresh