Snippet: validating xml with xsd in Java

     
import java.io.Reader;
import java.io.StringReader;


import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;


import org.springframework.core.io.ClassPathResource;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.xml.sax.SAXException;


....


String xmlFormated = "";
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

       ClassPathResource cpr = new ClassPathResource("my_xsd_file.xsd");
       Source schemaFile = new StreamSource( cpr.getFile()  );
       Schema schema = factory.newSchema(schemaFile);

       Validator validator = schema.newValidator();
       Reader reader = new StringReader(xmlFormated);
       Source source = new StreamSource(reader);


       try {
           validator.validate(source);
       } catch (SAXException e) {
        log.error(e);
       }


...

How to see maven modules as projects in Eclipse Package Explorer

When a user, not project's creator, downloads a maven project with modules inside. He/She sees a project that contains all modules. To get all modules as projects you can follow these tutorial:

1.- Download parent project
2.- On File menu, select import > Maven > Existing Maven Projects
3.- Push next
4.- Browse to downloaded project in Root Directory select list.
5.- Check pom.xml files listed below
6.- Push Finish

Now you can see the parent maven project and all modules as projects. All modules are synchronized with parent project.

Jasypt: Encryption on Java

Jasypt is a java library which allows the developer to add basic encryption capabilities to his/her projects with minimum effort, and without the need of having deep knowledge on how cryptography works.

  • High-security, standards-based encryption techniques, both for unidirectional and bidirectional encryption. Encrypt passwords, texts, numbers, binaries...
  • Transparent integration with Hibernate.
  • Suitable for integration into Spring-based applications and also transparently integrable with Spring Security.
  • Integrated capabilities for encrypting the configuration of applications (i.e. datasources).
  • Specific features for high-performance encryption in multi-processor/multi-core systems.
  • Open API for use with any JCE provider.

Copied from: http://www.jasypt.org
Adrià, I hope this help you in near future. And is Licensed under Apache Software License.

Changing Admin user in Alfresco 3.2 +...


...when you have no other administrator.

This is one of the ways to change Alfresco administrator, but there are another using groups.

Locate tomcat/shared/classes/alfresco/alfresco-global.properties

Add this property:

alfresco_user_store.adminusername=admin

And replace admin identifier by other user identifier.

Reboot Alfresco and login with new "admin" user.

How to transform all docs to PDF/A-1 format in Alfresco

Look for alfresco\WEB-INF\classes\alfresco\mimetype\openoffice-document-formats.xml file.

<document-format>
<name>Portable Document Format</name>
<mime-type>application/pdf</mime-type>
<file-extension>pdf</file-extension>
<export-filters>
<entry>
<family>Presentation</family>
<string>impress_pdf_Export</string>
</entry>
<entry>
<family>Spreadsheet</family>
<string>calc_pdf_Export</string>
</entry>
<entry>
<family>Text</family>
<string>writer_pdf_Export</string>
</entry>
</export-filters>
<!-- ADDED TO TRANSFORM TO PDF/A-1 -->
<export-options>
<entry>
<string>SelectPdfVersion</string>
<int>1</int>
</entry>
</export-options>
<!-- ADDED TO TRANSFORM TO PDF/A-1 -->
</document-format>


With this change, all transformation to PDF will be exported to format PDF/A-1.

Thanks to: http://forums.alfresco.com/en/viewtopic.php?t=6336
OpenOffice Parameters: http://wiki.services.openoffice.org/wiki/API/Tutorials/PDF_export

How to move window buttons to right side in Gnome

Open Configuration Editor (Alt+F2 and type in gconf-editor) and navigate to apps>metacity>general.
Look for button_layout on the right hand pane and edit it.

menu:minimize,maximize,close

Now buttons must be on right side

How to add twitter to a web page

First of all, we don't need a Twitter account to use widget in a page.

1.- Go to www.twitter.com
2.- At the bottom of the page press extras
3.- Select widgets
4.- Select My web
5.- Select 'Profile Widget'
6.- Introduce a profile and check configuration
7.- Copy and paste the code in your page:



PD: This post is a gift for my friend Juan Carlos ( a twitter addict ).

OpenSolaris, Postgresql, OpenOffice... what's next OpenJDK? (updated: Oracle + IBM)

"Is it time to consider replacing Java? Java pundit and former Editor-in-Chief of TheServerSide.com, Joseph Ottinger, steps into the fray by proposing some options, alternatives, and more to the point, a reality check."

http://www.theserverside.com/news/thread.tss?thread_id=61039

Update:
Oracle and IBM Collaborate to Accelerate Java Innovation Through OpenJDK


http://www.oracle.com/us/corporate/press/176988

How to enable full repository access on Alfresco Share

In a tomcat installation, edit tomcat/shared/classes/alfresco/web-extension/share-config-custom.xml file.

Look for RepositoryLibrary condition:

<config evaluator="string-compare" condition="RepositoryLibrary" replace="true">
<!--
Whether the link to the Repository Library appears in the header component or not.
-->
<visible>false</visible>

<!--
Root nodeRef for top-level folder.
-->
<root-node>alfresco://company/home</root-node>

<!--
Whether the folder Tree component should enumerate child folders or not.
This is a relatively expensive operation, so should be set to "false" for Repositories with broad folder structures.
-->
<tree>
<evaluate-child-folders>false</evaluate-child-folders>
</tree>
</config>

And modify visibility to true:

<visible>true</visible>


This works on Alfresco Enterprise 3.2R and above.

Are you ready for HTML5?


Here are results of browsers installed in my computer:

- Chromium 6.0.472.53: 217 / 300 + 10 bonus points - 1st - The best of breed!!
- Opera 10.70: 159 / 300 + 7 bonus points - 3rd - Opera can do it better
- Firefox 3.6.10: 139 / 300 + 4 bonus points - 4th - Mozilla where are you?

All test over Ubuntu linux 10.04

- Safari 5.0.1: 208 / 300 + 7 - hu-ha!! 2nd !!

Tested over MacOS X Snow Leopard (Ok, it's no my computer)

Check yourself at: http://html5test.com/
All test contain a detailed section explaining which points must improve.

What about IE8 / Windows7 ?

How to know your most used commands

There is an easy way to know how you spend your time on linux terminal:

history | awk '{print $2}' | sort | uniq -c | sort -rn | head -10

In my entries Alfresco appears on top 5. I think i need a shortcut.

Java Architecture for XML Binding

First of all we need to download and install JAXB in your pc. (See below for links)

After install, we can proceed defining a XSD file. An example jb.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:jb="urn:org:jblanco:example:expedient:1.0:schema"
xmlns:cmn="urn:org:jblanco:common:1.0:schema"
targetNamespace="urn:org:jblanco:example:expedient:1.0:schema"
elementFormDefault="qualified" attributeFormDefault="unqualified">

<xs:annotation>
<xs:documentation xml:lang="en">
Expedient.
</xs:documentation>
</xs:annotation>
<xs:import id="cmn" namespace="urn:org:jblanco:common:1.0:schema" schemaLocation="common.xsd" />
<xs:element name="expediente" type="jb:expedienteType" />
<xs:complexType name="expedienteType">
<xs:complexContent>
<xs:extension base="cmn:generalType" >
<xs:sequence>
<xs:element name="numero_expediente" minOccurs="1" maxOccurs="1" type="xs:string" />
<xs:element name="fecha_apertura" minOccurs="1" maxOccurs="1" type="xs:date" />
<xs:element name="fecha_cierre" minOccurs="1" maxOccurs="1" type="xs:date" />
<xs:element name="volumen" minOccurs="1" maxOccurs="1" type="xs:string" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>

</xs:schema>


Don't forget namespaces, inheritance... ;)

Finally, execute XJC command to generate java classes:

xjc -p org.jblanco.example.bean jb.xsd

xjc -p <package> xsd_file.xsd

Output:

jblanco@in2:/opt/tmp$ xjc -p org.jblanco.example.bean jb.xsd
parsing a schema...
compiling a schema...
org/jblanco/example/bean/ExpedienteType.java
org/jblanco/example/bean/ObjectFactory.java
org/jblanco/example/bean/package-info.java


To use multiple xsd files add an import, for every xsd you want, in the main file.

Hint:

To create an object with multiple inheritance, we can use group tag. An example:

<xs:complexType name="complex1Type">
<xs:sequence>
<xs:group ref="cmn:metaGrp1Group" />
<xs:group ref="cmn:metaGrp2Group" />
</xs:sequence>
</xs:complexType>


<xs:group name="metaGrp1Group">
<xs:sequence>
<xs:element name="identificador" minOccurs="1" maxOccurs="1" type="xs:string" />
<xs:element name="fecha" minOccurs="1" maxOccurs="1" type="xs:date" />
<xs:group ref="cmn:metaSubgrp1Group" />
</xs:sequence>
</xs:group>
<xs:group name="metaSubgrp1Group">
<xs:sequence>
<xs:element name="nivel" minOccurs="1" maxOccurs="1" type="cmn:nivelType" />
</xs:sequence>
</xs:group>
<xs:group name="metaGrp2Group">
<xs:sequence>
<xs:element name="codigo" type="xs:string" minOccurs="1" maxOccurs="1" />
<xs:element name="serie" type="xs:string" minOccurs="1" maxOccurs="1" />
</xs:sequence>
</xs:group>

<xs:simpleType name="nivelType">
<xs:restriction base="xs:string">
<xs:enumeration value="Unidad" />
<xs:enumeration value="Expediente" />
<xs:enumeration value="Compuesta" />
</xs:restriction>
</xs:simpleType>


Links used in this note: