Saturday, March 22, 2008

Generating Sample XML file from Schema

Generating Sample XML files from Schema

Eclipse XML Editor (comes with WTP Editor) has a nice feature to generate a sample xml file from the given schema.


Make sure you have the WTP plugin installed on your Eclipse editor – if not please check this or search in google.


Let us create a sample XML file for the following Schema (this schema file is created using Eclipse Schema editor – more details can be found here.)


<?xml version="1.0" encoding="UTF-8"?>

<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/shiporder" xmlns:tns="http://www.example.org/shiporder" elementFormDefault="qualified">


<element name="shiporder" type="tns:shiporderType"></element>

<complexType name="shiporderType">

<sequence>

<element name="orderperson" type="string"></element>

<element name="shipto" type="tns:shiptoType"></element>

<element name="item" type="tns:itemType" maxOccurs="unbounded" minOccurs="1"></element>

</sequence>

</complexType>


<complexType name="shiptoType">

<sequence>

<element name="street" type="string"></element>

<element name="city" type="string"></element>

<element name="country">

<simpleType>

<restriction base="string">

<enumeration value="INDIA"></enumeration>

<enumeration value="US"></enumeration>

<enumeration value="UK"></enumeration>

<enumeration value="Japan"></enumeration>

</restriction>

</simpleType>

</element>

<element name="pincode">

<simpleType>

<restriction base="int">

<pattern value="[0-9]{5}"></pattern>

</restriction>

</simpleType>

</element>

</sequence>

</complexType>

<complexType name="itemType">

<sequence>

<element name="title" type="string"></element>

<element name="quantity" type="int"></element>

<element name="price" type="double"></element>

</sequence>

</complexType>

</schema>



Assume that the above schema is stored in your project or in your system.


Now, to create a sample XML file for the above schema.


1) Create a New XML file using File -> New -> Others... and select XML and click next.




2) In the next wizard page, give the file name and the location where to create the file and click “Next”




3) Here give select the option “Create XML file from XML Schema” (if you want to create an XML file from DTD, you can select the first option. Click “Next” to choose the schema file.





4) In this wizard page, you need to give the schema file location. If your schema file is present in your workbench select the option “Select file from Workbench”. If your schema file is in your local system (but not in your workbench) you can import these files using “Import” button. However if you want to cretae an xml file from a standard schema file like WSDL or SOAP, you can choose “Select XML Catalog entry” - more on this later. Click “Next” button.




5) In this wizard you need to select the root element of you document. Let us select “shiporder” as our root element. You can check “Create optional attributes” and “Create optional elements” if you want to. However as our schema don't have any option attributes or element, you can simply ignore thos. Click “Finish” to create the XML file.







6) This will create an xml file like this




You can validate this XML file against the schema using the “Validate” option. Right click on the editor and select “Validate” option.


Observe that XML Validate found some errors in pincode tag.








It has an error saying

cvc-pattern-valid: Value '0' is not facet-valid with respect to pattern '[0-9]{5}' for type '#AnonType_pincodeshiptoType'.



This is because in our schema we defined the pincode to follow this pattern.


<element name="pincode">

<simpleType>

<restriction base="int">

<pattern value="[0-9]{5}"></pattern>

</restriction>

</simpleType>

</element>


Eclipse XML editor can't generate sample data based on pattern. It simply generate “0” for intergers, “0.0” for float or double and the tag name for strings. Ofcouse many comercial ides also can't generate data based on pattern restriction.


To fix this error manually give a valid value for pincode (a five digit value), save it and validate it again. The problem marker will go off if it is a valid value!



Working with XML Catalogs

Now we will look at the option “Select XML Catalog entry” to create a xml instead of creating an XML file from a give schema.





If you want to create an xml file from a standand schema like WSDL, SOAP you can select this option and in the XML Catalog you can select the key.


Let us select the key “http://schemas.xmlsoap.org/wsdl” and click Next. Select “definitions” as root element and click Finish. This will generate a file that will confirm to wsdl schema.




You can also add your custom schema to the XML Catalog entry so that whenever you want to create a XML file of that type you don't need to select the schema file again.


Now let us add our shiporder.xsd to XML Catalog.


Select Window > Preferences menu item. Here select Web and XML > XML Catalog as shown in figure.




Scroll down and select the “Add button” and choose either workspace or File system (wherever you schema file is present).




Select the workspace option and give your schema location and click Ok.





This will add you schema to XML Catalog




Click Ok to close the Preference setting dialog.


Now, you can create an XML file using this catalog entry. The first 3 three steps (till “Create XML file from Schema”) option is same. After this step Select “XML Catalog entry” and choose our shiporder.xsd and click next.




Click Next and then Click finish. This will create our require XML file.


The difference between the too files (one generated by directly giving the schema location) is the value of schemocation tag.


xsi:schemaLocation="http://www.example.org/shiporder shiporder.xsd "


xsi:schemaLocation="http://www.example.org/shiporder http://www.example.org/shiporder


In the first one first one the schema file is directly referenced using relative location, so if you schema file “shiporder.xsd” is not present in the directory where the xml file recides validation won't happen propely. However for file generated from XML catalog it is refering to a web location which is not present – but validation occurs fine as it uses the catalog to search for XML Schemas before going to the web address.


Suppose if you have 50 xml files that uses a schema and now if the schema needs to be moved to someother folder you need to update the schemaLocation attribute for all the 50 xml files if you have used the first option, but if you choosen to generate XML files from XML Catalog, only you need to update the catalog by selecting Edit option in your preferences page so all your 50 xml files automatically pick the correct schema – no need to update the xml files.


So, cataloging you XML Schemas is a better way if you what to repeadly use this schema.



Creating XML File from Templates

The New XML file wizard in Eclipse also have an option to generate XML files based on a template.


In the “Create XML File from” page select the “Create XML file from an XML template” option.




And click next, this open up the “Select XML Template” wizard page. Now select the “Use XML Template” option and click “XML Tempaltes” hyperlink.





This will open up the Preferences page




Click on “New...” button to create a new template.




Give a name “shipOrder” and decription. Also select the Context as “New XML” so this template will be used while creating new xml files.


In the pattern file, type (or copy and paste) the XML you want. You can also add variables to the pattern using Inset Variable... button.


Clikc OK once you are done.


Now our shipOrder will be shown in the template list as shown in the figure (note that only templates with context New XML will be displayed here).



Select our newly created template “shipOrder” and click finish.


This will create an XML file using that template.


Thursday, March 20, 2008

Eclipse Schema Editor

Working with schema files in Eclipse


A schema (XSD) is a complex language for describing the structure of your XML data. Writing or modify a schema file is quite difficult, but most the times it is required to write a new schema or understand an existing file because it the only widely used standard for describing xml structure (others are: 1) DTD, which is not used these days as it has lot of short commings including suport for namespaces 2) Relax NG). However schemas are widely used in Web services (for describing the structure of input data a service can accept – specified in wsdl types section). Check this link for more details about different languages for describing XML document http://www.xml.com/pub/a/2001/12/12/schemacompare.html

However Eclipse WTP plugin has a nice graphical Schema (XSD) editor that allows you to create, view and edit a schema file easily, effectively and without much pain in understanding the schema language.


This presentation gives you a basic introduction and features present in schema editor.


1) Configuring Eclipse

If Eclipse is not readily available on your system, you can download it from http://www.eclipse.org/downloads/, download “Eclipse IDE for J2EE development”.

Check and Install Schema editor

Click Help -> About Eclipse platform -> plugin details button





Verify that there is a plugin with name “XML Schema Editor”. If Schema editor is not installed please follow the below steps to install a Schema editor.


Installing a WTP plugin if not installed

The best way to install eclipse plugins is to use “Software updates” instead of manully downloading zip file and extracting it to the eclipse folder. “Software updates” not only downloads the dependent plugins but also downloads the correct version of the plugin based on your current version of Eclipse.

Help -> Software updates -> Find and Install this open up “Install/Update” dialog. “Choose search for new features to install” option and click next, select “Web Tools platform” and click finish. This will download the latest version of WTP plugins for your Eclipse enviroment.


2) Creating a new schema file

Now we try to create a schema file for a shipment notice data (sample data is shown in figure). Even though there are tools that allows to generate schema from a sample xml file (available in XML Spy and Stylus studio), the idea here to familarize with Eclipse schema editor.


<?xml version="1.0" encoding="ISO-8859-1"?>

<shiporder orderid="889923"

xmlns="urn:shiporder">

<orderperson>John Smith</orderperson>

<shipto>

<name>Ola Nordmann</name>

<address>

<street>Langgt 23</street>

<city>4000 Stavanger</city>

<country>Norway</country>

<pincode>35004</pincode>

</address>

</shipto>

<item seq="1">

<title>Empire Burlesque</title>

<note>Special Edition</note>

<quantity>1</quantity>

<price>10.90</price>

</item>

<item seq="2">

<title>Hide your heart</title>

<quantity>1</quantity>

<price>9.90</price>

</item>

</shiporder>


The shiporder is the root tag that has a orderid attribute and it can has detailed about name of the person who ordered the items (orderperson) and his address (address tag). It also contains the items he has order (item tag, he can order different items using same request – hence item tag can be one or more). Address contains an street, city, country and pincode. Let us assume that country is an optional tag and if it is not given the country will be US. Also the country is fixed list like US, UK, India, Japan. Pincode is five digit number. The item has an seq attribute (which is interger and a unique), title, quantity (a positive five digit integer) and price (a positive double value).


Now let us see how we construct a xml schema using the above constaints using Eclipse WST editor.


I assume you don't have in depth knowledge about xml schemas (that is the reason why you and I should use an Editor – a person who wrote the schema spec can easily write schemas using notepad :-). However basic idea about schemas is required – able to identify an Element, Type (complexType or simpleType) and attributes.


1) Create a new project, XMLSamples – using File -> New project and selecting “Project” under the General section




2) Click next, and type “XMLSample” in Project name field and click Finish – This will create a new Eclipse project named “XMLSample”





3) Create a new Schema file, by using new file wizard: File -> New -> Others and selecting “XML Schema” under XML section as shown in figure.



4) Click next and type “shiporder.xsd” and click finish.




This will create a file “shiporder.xsd” in the XMLSamples project and opens in the editor. Go the design view page (if is opened in source page).




The Eclipse schema editor has five section in the Design page

  1. Directives – Other schema files that are included or improted in this schema will be listed here.

  2. Elements – All elements that the defined this schema will be shown here

  3. Types – All types that the defined in this schema will be shown here

  4. Attributes – Attributes defined will be listed in this section

  5. Groups – Goups will be listed here.


In addition to the Design page, you need to know about Outline view and Properties view.

If these views are not visible, you can bring them up by selecting Window -> Show view -> Others and selecting Outline view and clicking ok (same way to bring the Properties view also) – we see later how to use these views.




Now, let us start adding element to our schema file.


The root tag should be shiporder that contains orderperson, shipto and item tags. Also the schema elements should be in the namespace “urn:shiporder”

Right click the Element section and select Add Element menu item. Make the element names as “shiporder”.




You can rename the element name by click on the name text (or using Properties view).


By default XSD Editor creates a element of type string, but as shiporder contains others tags like ordername, so let us change the type of the shiporder.
























This will open up the New type dialog, create a complex type with name “shiporderType”


Now double click the shiporderType item present in the Types section. Right click on the shiporderType and select “Add Element”. This will add a new element of type string. Add three element and name it as “orderperson”, “shipto” and “item” - same names as the element tags we want inside the shiporder tag.




But again our shipto and item tags are of complex type not simple type like string or int. So, right click on the shipto column and select menu item Step type -> New, this open up the “New type” dialog we saw previously. Name it as shiptoType. Similarly do it for item also.



The final output should look like:


Now we need to add street, city, country and pincode for shiptoType and title, quantity and price for itemType.

You can do it by right click on the shiptoType and selecting “Add Element”


To change the type from string to int, you can click on the type and select int as shown in figure.


Adding all the tags the screen will look like:


By now we have created all the require tags.


Adding constraints


item” can occur one or more time in the shiporder. To make this right click on the item and select “Set multiplicity -> 1...*” menu item



Alternatively you can change the Min occurance and max occurance using properties view. If properties view is not visible, can make it appear by selecting the menu item “Window -> Show view -> Others” and select “Properties” in the general section..




This will open the Properties view.


Click on each item will update the properties view to show the properties of that selected item.

When we select the item in Design view the properties will look like:




You can set the minimum occurrence and maximum occurrence of the element.


Also our pincode should be a five digit interger. To apply this contraint, click on the “pincode” and select the “constraints” tab in properties view. In the “Specify constraint values” group choose “Patterns” option and click “Add” button.



This will bring up the “Regular Expression wizard”, type the regualar expression “[0-9]{5}” which specifies any letter from 0 to 9 and should occur five times. Click next to test your regualar expression and then click “Finish”.
































































This will make the pincode to be an integer with only five characters (or digits).


To make the quantity a positive integer, click on the “quantity” tag and in the constraints tab of properties view, give minimum value as “0”.


You can do the same for price also to make it a positive double.




Finally let us make our country tag to accept only a fixed set of values like “US, UK, France, Germany, Japan and India” using the Properties view.


To do this, click on the country item in the Design page and in the properties view select “Constraints” tab and choose “Enumerations” in the “Specific constraint values” group box. Click on Add and give a name “India”, to add one more item to the list, click “Add” button again and give “US” as value this time.




Now save the file (using Ctrl + s keyboard short cut), you can view the source by click on the source tab:




This completes our creating schemas using XML Schema editor.


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.

Speed up the start of Acrobat Reader

Opening PDF files are taking time, then

Method 1:
Every time you run Adobe Acrobat, up to 20 plugins are loaded
unnecessarily - most users do not need even a fraction of them!
To disable unneeded plugins and make them optional instead, follow these
instructions:
1. Browse to the plugins folder: C:\Program Files\Adobe\Acrobat
7.0\Reader\plug_ins
2. Create a new folder named Optional
3. Move all files from the plug_ins folder to Optional, except
EWH32.api, print*.api, and Search*.api

Method 2:
Download the software Adobe Reader SpeedUp software from
http://www.tnk-bootblock.co.uk/software/, which does the same thing
specified in Method 1, but in a nice GUI friendly option.

Method 3:
Install a light weight PDF reader (other than Acrobat). The best free
PDF reader is Foxit Reader. No need to install, just copy the executable
and run from your system. This is very light and extremely fast.

Foxit reader is available at
http://us01.foxitsoftware.com/foxitreader/foxitreader.zip (just 1.8 MB
size).


Threading libraries in C++

Here is the list of Threading libraries in C++

Pthread (POSIX threads)
http://en.wikipedia.org/wiki/POSIX_Threads
pthread won't work fine on windows, because windows won't support POSIX standards - but there are some implementations of pthreads api on windows also!
Note: pthread is a c library not a c++ library, hence you can use it in c applications also.

Boost::thread - supports many compilers and architectures include Linux
and Windows.

Check these links: http://www.ddj.com/cpp/184401518,
http://www.boost.org/doc/html/thread.html

The problem with Boost libraries is that it requires good knowledge on STL, and learning is not very easy.

Glib::thread - Glib supports multi-threading, it can run on Windows and Linux but I don't think it supports VC++ compiler.

Check these:
http://www.gtkmm.org/docs/glibmm-2.4/docs/reference/html/classGlib_1_1Th
read.html, http://inti.sourceforge.net/tutorial/libinti/threads.html

Qt: Qt library (not the Quick Time player from Apple) also have threading support (both 3.x and 4.x versions). Qt 4.x is released under GPL for Windows also.

http://doc.trolltech.com/3.3/threads.html

Note: Nokia recently acquired TrollTech (the company behind Qt)

Intel threading library:
One more library from Intel (open source library, GPL2 with the runtime
exception). But only works on x86 or x86_64 (Intel or AMD; 32 or 64 bit)
architectures.
http://www.intel.com/software/products/tbb/
http://threadingbuildingblocks.org

wxWidgets also have a threading library.

And it is easy to write own threading library too!

Win32 is also having a thread api - but who is using win32 or MFC these days?

However I bet on either PThreads or Boost::Threads.

Anyhow in next version <http://en.wikipedia.org/wiki/C%2B%2B0x> of C++, standard library will support threading - API very similar to boost::thread library!

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