Posts Tagged ‘xml’

Glassfish 4.0 : “Hello world” application with Maven 3

The following steps will allow you to develop and deploy a first demo web-app with Glassfish Server 4.
1) To install Glassfish 4.0 follow the steps at
For example, you can get the zip version and extract into: C:\glassfish4.

2) If you´re working on a Windows Desktop Rename glassfish4/glassfish/bin asadmin to *.s to prevent the following Maven error:
Unable to start domain "domain1". IOException: Cannot run program"C:\glassfishv3\glassfish\bin\asadmin": CreateProcess error=193, %1 is not a valid Win32-Application

In this way The system will run asadmin.bat, instead of calling the Linux script (provided with no file extension by Glassfish).

3) On Eclipse install Glassfish plugin ( to manage the server graphically. Otherwise you would not be able to installing with the command File>New>Other>Server> etc.
Starting Glassfish for the first time, you will have a user called “admin” with no password. You can manage Glassfish with Admin-Console available at: http://localhost:4848/ or even with asadmin CLI console (under C:\glassfish4\glassfish\bin).

4) To deploy your application using Maven, create the settings.xml file under the .m2 folder (for example, under Windows at the location: C:\Users\username\.m2), providing the glassfish profile credentials: they will automatically imported in the pom.xml file.

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

5) Create a new Maven project with the maven archetype “maven-archetype-webapp” template. As a default you can already find the index.jsp HelloWorld page.

6) Add the following plugins to the pom.xml:


					<!-- <webXml>src\main\webapp\WEB-INF\web.xml</webXml> -->



7) Launch the maven command:
clean package glassfish:deploy

You may find errors related to the Dynamic Web Module in the Eclipse project, even if the maven gives you “BUILD SUCCESS”. For now it´s ok.

With the given pom.xml the application will be deployed into:

You can see it up and running at: http://localhost:8080/Glassfish_HelloWorld/

You can manage the applications, for example editing the Content Root folder, directly in the administration console.

Demo Code available at:

JAXB part 8: generate classes from DTD with maven

After using JAXB with XSD schema, I have finally discovered that it´s MUCH better to make it with files and jaxd binding files.

Supposing you want to generate the classes for a web-app xml file, in the Maven pom.xml you need to add something like:

		<!-- if you want to put DTD somewhere else <schemaDirectory>src/main/jaxb</schemaDirectory> -->

In the specified schema directory there must be both the .dtd and the .jaxb binding files.
To specify a package for the generated classes, in the jaxb file like:

<?xml version="1.0" ?>
     <xml-java-binding-schema xmlns:xjc=""
         <options package="deploy.service.webapp" />

You can find a full demo of a web-app xml schema classes generation at sourceforge: JAXB DTD maven demo

JAXB part 5: XML Character handler

With JAXB you need to dhandle special characters you can just choose between two options.

Otherwise you need to define your own character handler.

The following class shows you how to handle both CDATA elements and special characters.


import com.sun.xml.bind.marshaller.CharacterEscapeHandler;

public class XmlCharacterHandler implements CharacterEscapeHandler {

	public void escape(char[] buf, int start, int len, boolean isAttValue,
			Writer out) throws IOException {
		StringWriter buffer = new StringWriter();

		for (int i = start; i < start + len; i++) {

		String st = buffer.toString();

		if (!st.contains("CDATA")) {
			st = buffer.toString().replace("&", "&amp;").replace("<", "&lt;")
					.replace(">", "&gt;").replace("'", "&apos;")
					.replace("\"", "&quot;");

//		System.out.println(st);


In your marshaller method just add the following property:

					new XmlCharacterHandler());

JAXB – PART 3 : (un)marshalling xml file with DOCTYPE declaration

Although many XML files have a DOCTYPE declaration, browsing the internet and my safary library account haven´t helped that much to unmarshall (and marshall) my mbeans descriptors with JAXB. It tooks me several hours to find a way out.
Supposing that the xml root element is called “Bean”, you can get the header, simply converting the xml file in a string and taking all the content until the first root element tag (). This might be very useful if you want to scan a folder in which the xml files have different headers (like jboss services configurationes ones).

	public static String getHeader(String filePath) {

		String header = "";
		String xmlContent = "";
		int headerLastIndex = 0;

		File file = new File(filePath);

		String text = "";
		try {
			text = FileUtils.readFileToString(file, "UTF-8");

			headerLastIndex = text.indexOf("<mbean>");
			header = text.substring(0, headerLastIndex);

		} catch (IOException e) {
			// TODO Auto-generated catch block

		return header;

	public static void marshaller(Mbean mbean, String filePath, String header)
			throws CustomizerException {

		stringWriter = new StringWriter();

		File file = new File(filePath);
		try {

			jaxbContext = JAXBContext.newInstance(Mbean.class);

			fileWriter = new FileWriter(file);

			marshaller = jaxbContext.createMarshaller();
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
			marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

			marshaller.marshal(mbean, stringWriter);




		} catch (MarshalException ex) {

			throw new CustomizerException("Marshalling the file: " + filePath
					+ "not possible. Wrong file content");

		} catch (JAXBException e) {

			throw new CustomizerException("Could not parse the XML file: "
					+ filePath);

		} catch (IOException e) {

			throw new CustomizerException("Couldn´t access the file: "
					+ filePath);

		finally {

			try {
			} catch (IOException e) {

				throw new CustomizerException(
						"Couldn´t close the fileWriter for: " + filePath);


I would like to make it in a better way with JAXB, but I think that the second version is still a bit raw. Currently there are no unmarshalling properties provided. You can invoke the method setProperty(java.lang.String name, java.lang.Object value), but there are still no properties provided by the API. I would like to contribute to the project…

JAXB – TEAM EXAMPLE – PART 1: XSD schema with root attribute and child element with attribute and value

In this post I will show you how to represent an XSD schema with root attribute and child element with attribute and value. For example, let´s consider a software development team:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<team category="software development">
    <Member role="junior">Laura</Member>
    <Member role="senior">Erik</Member>
    <Member role="graduate">Mike</Member>

The xsd schema generally represent a a working team :

<xsd:schema xmlns:xsd=""
	targetNamespace="" xmlns:team=""

	<xsd:element name="Team" type="team:Team" />

	<xsd:complexType name="Team">
			<xsd:documentation>Represents a working team </xsd:documentation>
		<xsd:sequence maxOccurs="unbounded">
			<xsd:element name="Member">
						<xsd:extension base="xsd:string">
							<xsd:attribute name="role" type="xsd:string" />
		<xsd:attribute name="category" type="xsd:string" />

Although it seems to be a typical case, I have spent several hours reading books, tutorials and forum, trying to map a web service xml file with such a structure.
I have finally found the solution on a google forums post.

A better solution would be using the type attribute and make a separate “complexType” tag, because in this way JAXB would be able to create a different bean class for each element (also the nested ones). So an equivalent version of the schema is the following:

<xsd:schema xmlns:xsd=""
	targetNamespace="" xmlns:team=""

	<xsd:element name="Team" type="team:Team" />

	<xsd:complexType name="Team">
			<xsd:documentation>Represents a working team </xsd:documentation>
			<xsd:element name="Member" type="team:member" minOccurs="0"
				maxOccurs="unbounded" />
		<xsd:attribute name="category" type="xsd:string" />
	<xsd:complexType name="member">
			<xsd:extension base="xsd:string">
				<xsd:attribute name="role" type="xsd:string" />


In eclipse, downloading the “m2e conntector for jaxb” plugin, you can automatically generate all the object classes from your XSD schema (in the menu bar menu you can find the JAXB section under File>New>…). Without a binding file, you will have to add the “@XmlRootElement” annotation on the top of your root element class and also delete the namespace in the package-info class. I will tell you more in the next posts…

Liferay Service Builder with external database

To make a Liferay Service Builder with external database there are some rules to follow. This post will help you to get straight to the point.
Keep in mind that:

  • You can’t deploy more than one portlet mapping column with the same entity name, otherwise you get a hot-Deployment error.
  • the names of the portlet name, namespace and database must be distinct (ex. portlet=”imageview“, namespace=”mammography“, database=”dicomviewer“).
  • when you add external jar, you must add them in the Tomcat/lib/ext
    so liferay can find them in the global classpath.

The steps to make the service builder with the external database mapping are:

  1. make the service.xml file in the /docroot/WEB-INF/
  2. launch the build-service ant command
  3. add the ext-spring.xml file in the /docroot/WEB-INF/src/META-INF/ folder
  4. deploy the portlet.

The example below might be helpful.

—service.xml ————

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.0.0//EN" "">

<service-builder package-path="it.dicom">
<author>Laura Liparulo</author>

<entity name="Volume" local-service="true" remote-service="true" table="volume"
data-source="mammographyDataSource" session-factory="mammographySessionFactory" tx-manager="mammographyTransactionManager">
<!-- PK Fields -->
<column name="volumeId" type="long" primary="true" />
<!-- Other Fields -->
<column name="volumeName" type="String" />
<!-- Relationships -->
<column name="caseVolume" type="Collection" entity="CaseArchive"
mapping-key="volumeId" />
<order by="asc">
<order-column name="volumeName" />

<finder name="Volume_Name" return-type="Collection">
<finder-column name="volumeName" />

<entity name="CaseArchive" local-service="true" remote-service="true" table="case_archive"
data-source="mammographyDataSource" session-factory="mammographySessionFactory" tx-manager="mammographyTransactionManager">
<!-- PK Fields -->
<column name="caseId" type="long" primary="true" />
<!-- Other Fields -->
<column name="caseName" type="String" />
<column name="volumeId" type="long" />
<column name="notes" type="String" />
<!-- Relationships -->
<column name="image_Case" type="Collection" entity="Image"
mapping-key="caseId" />
<order by="asc">
<order-column name="caseName" />
<finder name="Case_Name" return-type="Collection">
<finder-column name="caseName" />



—ext-spring.xml ————

<?xml version="1.0"?>
<bean id="mammographyDataSourceTarget"
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/dicomviewer" />
<property name="username" value="root" />

<property name="password" value="tigertailz" />
<bean id="mammographyDataSource"
<property name="targetDataSource">
<ref bean="mammographyDataSourceTarget" />
<bean id="mammographyHibernateSessionFactory"
<property name="dataSource">
<ref bean="mammographyDataSource" />
<bean id="mammographySessionFactory">
<property name="sessionFactoryImplementor">
<ref bean="mammographyHibernateSessionFactory" />

<bean id="mammographyTransactionManager"
<property name="dataSource">
<ref bean="mammographyDataSource" />
<property name="sessionFactory">
<ref bean="mammographyHibernateSessionFactory" />

Now you can make it fast as a shark! 🙂