Skip to content

Commit

Permalink
Merge pull request spring-projects#36 from ghillert/INTEXT-59
Browse files Browse the repository at this point in the history
INTEXT-59 - Add XQuery Sample
  • Loading branch information
ghillert committed Mar 29, 2013
2 parents c374ac3 + c879ef5 commit 3063278
Show file tree
Hide file tree
Showing 9 changed files with 584 additions and 0 deletions.
103 changes: 103 additions & 0 deletions samples/xquery/README.md
@@ -0,0 +1,103 @@
Spring Integration - XQuery Sample
==================================

## Overview

This sample executes a simple [XQuery][] script. It uses the *[Spring Integration XQuery][]* module, which is part of the [Spring Integration Extensions][] project. The sample uses one of the following 3 XQuery processors:

* [Saxon][]
* [Sedna][]
* [BaseX][]

The example for [Saxon][] will work out of the box. [Sedna][] and [BaseX][] are in fact [XML Databases][], which run as external processes. For [Sedna][]- and [BaseX][]-specific setup instructions, please visit:

* http://xqj.net/sedna/
* http://xqj.net/basex/

## Running the Sample

From the command line execute:

$ mvn clean package

followed by:

$ mvn exec:java

The application should start up and you will see the following screen on which you can choose which XQuery processor to use:

=========================================================

Welcome to the Spring Integration XQuery Sample!

For more information please visit:
http://www.springintegration.org/

=========================================================
Which XQuery Processor would you like to use? <enter>:
1. Use Saxon
2. Use Sedna
3. Use BaseX
q. Quit the application
Enter you choice:

### Note regarding BaseX

The [BaseX][] dependency conflicts with [Sedna][]. Therefore, this sample applies a separate Maven profile for [BaseX][]. If not triggering that special profile when you try to execute the [BaseX][] example, you may encounter the following error message:

Detected the Sedna library to be present. This conflicts with BaseX. Please start the application from the command line using:

mvn exec:java -Dbasex

Consequently, execute from the command line:

$ mvn exec:java -Dbasex

The [BaseX][] sample will now execute successfully.

## Details

The used XQuery Script, located under `src/main/resources/data/xquery.xql`, is quite simple:

<customers>
{ //customers/customer/name }
</customers>

All it does is extracting the customer names from the following XML document:

<customers>
<customer id="1">
<name>Foo Industries</name>
<industry>Chemical</industry>
<city>Glowing City</city>
</customer>
<customer id="2">
<name>Bar Refreshments</name>
<industry>Beverage</industry>
<city>Desert Town</city>
</customer>
<customer id="3">
<name>Hello World Services</name>
<industry>Travel</industry>
<city>Coral Sands</city>
</customer>
</customers>


The XML document is located at `src/main/resources/data/customers.xml`.

The resulting XML document should look like:

<customers>
<name>Foo Industries</name>
<name>Bar Refreshments</name>
<name>Hello World Services</name>
</customers>

[Saxon]: http://saxon.sourceforge.net/
[Sedna]: http://www.sedna.org/
[BaseX]: http://basex.org/
[Spring Integration XQuery]: https://github.com/SpringSource/spring-integration-extensions/tree/master/spring-integration-xquery
[Spring Integration Extensions]: https://github.com/SpringSource/spring-integration-extensions
[XML Databases]: http://en.wikipedia.org/wiki/XML_database
[XQuery]: http://en.wikipedia.org/wiki/XQuery
153 changes: 153 additions & 0 deletions samples/xquery/pom.xml
@@ -0,0 +1,153 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.springframework.integration.samples</groupId>
<artifactId>xquery</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Samples (Basic) - XQuery Sample</name>
<url>http://www.springsource.org/spring-integration</url>

<prerequisites>
<maven>2.2.1</maven>
</prerequisites>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.integration.version>2.2.0.RELEASE</spring.integration.version>
<log4j.version>1.2.17</log4j.version>
<junit.version>4.10</junit.version>
</properties>

<repositories>
<repository>
<id>repo.springsource.org.milestone</id>
<name>Spring Framework Maven Snapshot Repository</name>
<url>https://repo.springsource.org/libs-snapshot</url>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>

<build>
<plugins>
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<additionalProjectnatures>
<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
</additionalProjectnatures>
<additionalBuildcommands>
<buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
</additionalBuildcommands>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>org.springframework.integration.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>

<!-- Testing -->

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

<!-- Spring Integration -->

<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-core</artifactId>
<version>${spring.integration.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-xml</artifactId>
<version>${spring.integration.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-xquery</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
</dependency>

<!-- Logging -->

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>

<!-- Saxon -->

<dependency>
<groupId>net.sf.saxon</groupId>
<artifactId>Saxon-HE</artifactId>
<version>9.4</version>
</dependency>
</dependencies>
<profiles>
<profile>
<id>default</id>
<activation>
<property><name>!basex</name></property>
</activation>
<dependencies>
<dependency>
<groupId>net.xqj.sedna</groupId>
<artifactId>sedna-xqj</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.xqj2</groupId>
<artifactId>xqj2</artifactId>
<version>0.1.0</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>basex</id>
<activation>
<property><name>basex</name></property>
</activation>
<dependencies>
<dependency>
<groupId>net.xqj</groupId>
<artifactId>basex-xqj</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
</profile>
</profiles>

</project>
133 changes: 133 additions & 0 deletions samples/xquery/src/main/java/org/springframework/integration/Main.java
@@ -0,0 +1,133 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration;

import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;

import org.apache.log4j.Logger;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.integration.service.CustomerService;
import org.springframework.util.ClassUtils;
import org.springframework.util.FileCopyUtils;


/**
* Starts the Spring Context and will initialize the Spring Integration routes.
*
* @author Gunnar Hillert
* @version 1.0
*
*/
public final class Main {

private static final Logger LOGGER = Logger.getLogger(Main.class);

private static final String HORIZONTAL_LINE = "=========================================================\n";

private Main() { }

/**
* Load the Spring Integration Application Context
*
* @param args - command line arguments
* @throws IOException
*/
public static void main(final String... args) throws IOException {

final Scanner scanner = new Scanner(System.in);

System.out.println("\n"
+ HORIZONTAL_LINE
+ "\n "
+ "\n Welcome to the Spring Integration XQuery Sample! "
+ "\n "
+ "\n For more information please visit: "
+ "\n http://www.springintegration.org/ "
+ "\n "
+ "\n"
+ HORIZONTAL_LINE);

System.out.println("Which XQuery Processor would you like to use? <enter>: ");
System.out.println("\t1. Use Saxon");
System.out.println("\t2. Use Sedna");
System.out.println("\t3. Use BaseX");

System.out.println("\tq. Quit the application");
System.out.print("Enter your choice: ");

final GenericXmlApplicationContext context = new GenericXmlApplicationContext();

while (true) {
final String input = scanner.nextLine();

if("1".equals(input.trim())) {
context.getEnvironment().setActiveProfiles("saxon");
System.out.println("\nUsing Saxon...");
break;
} else if("2".equals(input.trim())) {
context.getEnvironment().setActiveProfiles("sedna");
System.out.println("\nUsing Sedna...");
break;
} else if("3".equals(input.trim())) {
context.getEnvironment().setActiveProfiles("basex");
System.out.println("\nUsing BaseX...");

if(ClassUtils.isPresent("net.xqj.sedna.SednaXQDataSource", ClassUtils.getDefaultClassLoader())) {
LOGGER.error("Detected the Sedna library to be present. This "
+ "conflicts with BaseX. Please start the application "
+ "from the command line using:\n\n"
+ "mvn exec:java -Dbasex\n\nExiting...");
System.exit(1);
}

break;
} else if("q".equals(input.trim())) {
System.out.println("Exiting application...bye.");
System.exit(0);
} else {
System.out.println("Invalid choice\n\n");
System.out.print("Enter you choice: ");
}
}

context.load("classpath:META-INF/spring/integration/*-context.xml");
context.registerShutdownHook();
context.refresh();

final CustomerService service = context.getBean(CustomerService.class);

final Resource resource = context.getResource("classpath:data/customers.xml");
final InputStream is = resource.getInputStream();
final String customers = new String(FileCopyUtils.copyToByteArray(is));

System.out.println("\n\nExtracting Customer Names from:\n"
+ HORIZONTAL_LINE
+ customers + "\n"
+ HORIZONTAL_LINE
+ "\n");
final String customernames = service.getCustomerNames(customers);
System.out.println("Extracted Customer Names: \n"
+ HORIZONTAL_LINE
+ customernames + "\n"
+ HORIZONTAL_LINE
+ "\n");

}

}

0 comments on commit 3063278

Please sign in to comment.