From 4735c700b1bfe7ab81102a9f89109f333f5a8f34 Mon Sep 17 00:00:00 2001 From: Jonas Partner Date: Sat, 22 Nov 2008 21:49:24 +0000 Subject: [PATCH] sample for INT-432 documentation to follow --- .../xml/BookOrderProcessingSample.java | 64 +++++++++++++++ .../samples/xml/ExternalResupply.java | 26 ++++++ .../integration/samples/xml/StockChecker.java | 46 +++++++++++ .../samples/xml/WarehouseDispatch.java | 28 +++++++ .../integration/samples/xml/XmlUtil.java | 39 +++++++++ .../xml/bigBooksSupplierTransformer.xsl | 18 +++++ .../integration/samples/xml/order.xml | 11 +++ .../samples/xml/orderProcessingSample.xml | 80 +++++++++++++++++++ 8 files changed, 312 insertions(+) create mode 100644 org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/xml/BookOrderProcessingSample.java create mode 100644 org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/xml/ExternalResupply.java create mode 100644 org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/xml/StockChecker.java create mode 100644 org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/xml/WarehouseDispatch.java create mode 100644 org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/xml/XmlUtil.java create mode 100644 org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/xml/bigBooksSupplierTransformer.xsl create mode 100644 org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/xml/order.xml create mode 100644 org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/xml/orderProcessingSample.xml diff --git a/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/xml/BookOrderProcessingSample.java b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/xml/BookOrderProcessingSample.java new file mode 100644 index 0000000000..3f8d08d536 --- /dev/null +++ b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/xml/BookOrderProcessingSample.java @@ -0,0 +1,64 @@ +/* + * Copyright 2002-2008 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.samples.xml; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +import org.springframework.context.support.AbstractApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; +import org.springframework.integration.core.MessageChannel; +import org.springframework.integration.message.GenericMessage; +import org.w3c.dom.Document; + +/** + * + * @author Jonas Partner + * + */ +public class BookOrderProcessingSample { + + public static void main(String[] args) throws Exception { + try { + AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext( + "orderProcessingSample.xml", BookOrderProcessingSample.class); + MessageChannel messageChannel = (MessageChannel) applicationContext.getBean("ordersChannel"); + GenericMessage orderMessage = createXmlMessageFromResource("org/springframework/integration/samples/xml/order.xml"); + messageChannel.send(orderMessage); + applicationContext.close(); + } + catch (Throwable t){ + t.printStackTrace(); + } + finally { + System.exit(0); + } + } + + private static GenericMessage createXmlMessageFromResource(String path) throws Exception { + Resource orderRes = new ClassPathResource(path); + + DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); + builderFactory.setNamespaceAware(true); + DocumentBuilder builder = builderFactory.newDocumentBuilder(); + + Document orderDoc = builder.parse(orderRes.getInputStream()); + return new GenericMessage(orderDoc); + } + +} diff --git a/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/xml/ExternalResupply.java b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/xml/ExternalResupply.java new file mode 100644 index 0000000000..c6bde97bd1 --- /dev/null +++ b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/xml/ExternalResupply.java @@ -0,0 +1,26 @@ +/* + * Copyright 2002-2008 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.samples.xml; + +import org.w3c.dom.Document; + +public class ExternalResupply { + + public void orderResupply(Document resupplyOrder){ + System.out.println("Placing resupply order: " + XmlUtil.docAsString(resupplyOrder)); + } + +} diff --git a/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/xml/StockChecker.java b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/xml/StockChecker.java new file mode 100644 index 0000000000..c57f2bbfb5 --- /dev/null +++ b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/xml/StockChecker.java @@ -0,0 +1,46 @@ +/* + * Copyright 2002-2008 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.samples.xml; + +import org.springframework.xml.xpath.XPathExpression; +import org.w3c.dom.Document; + +/** + * + * @author Jonas Partner + * + */ +public class StockChecker { + + private final XPathExpression isbnSelectingXPath; + + public StockChecker(XPathExpression isbnSelectingXPath) { + this.isbnSelectingXPath = isbnSelectingXPath; + } + + public Document checkStockLevel(Document doc) { + String isbn = isbnSelectingXPath.evaluateAsString(doc); + boolean inStock = false; + + // we only carry stock of one book currently + if ("0321200683".equals(isbn)) { + inStock = true; + } + doc.getDocumentElement().setAttribute("in-stock", String.valueOf(inStock)); + return doc; + } + +} diff --git a/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/xml/WarehouseDispatch.java b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/xml/WarehouseDispatch.java new file mode 100644 index 0000000000..e74192d019 --- /dev/null +++ b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/xml/WarehouseDispatch.java @@ -0,0 +1,28 @@ +/* + * Copyright 2002-2008 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.samples.xml; + +import org.w3c.dom.Document; + +public class WarehouseDispatch { + + public void dispatch(Document orderItem){ + System.out.println("Warehouse dispatching orderItem: " + XmlUtil.docAsString(orderItem)); + } + + + +} diff --git a/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/xml/XmlUtil.java b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/xml/XmlUtil.java new file mode 100644 index 0000000000..ce0e8956c1 --- /dev/null +++ b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/xml/XmlUtil.java @@ -0,0 +1,39 @@ +/* + * Copyright 2002-2008 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.samples.xml; + +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; + +import org.springframework.xml.transform.StringResult; +import org.w3c.dom.Document; + +public class XmlUtil { + + public static String docAsString(Document doc) { + try { + StringResult res = new StringResult(); + Transformer transformer = TransformerFactory.newInstance().newTransformer(); + transformer.transform(new DOMSource(doc), res); + return res.toString(); + } + catch (Exception e) { + throw new RuntimeException(e); + } + } + +} diff --git a/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/xml/bigBooksSupplierTransformer.xsl b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/xml/bigBooksSupplierTransformer.xsl new file mode 100644 index 0000000000..0791aff407 --- /dev/null +++ b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/xml/bigBooksSupplierTransformer.xsl @@ -0,0 +1,18 @@ + + + + + + smallbooks + 5 + + + + + + + + \ No newline at end of file diff --git a/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/xml/order.xml b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/xml/order.xml new file mode 100644 index 0000000000..cb4c48cbbe --- /dev/null +++ b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/xml/order.xml @@ -0,0 +1,11 @@ + + + + 0321200683 + 2 + + + 1590596439 + 1 + + \ No newline at end of file diff --git a/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/xml/orderProcessingSample.xml b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/xml/orderProcessingSample.xml new file mode 100644 index 0000000000..0b9fec1024 --- /dev/null +++ b/org.springframework.integration.samples/src/main/java/org/springframework/integration/samples/xml/orderProcessingSample.xml @@ -0,0 +1,80 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +