|
| 1 | +/* |
| 2 | + * Copyright 2008 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.ws.samples.pox.ws; |
| 18 | + |
| 19 | +import javax.xml.parsers.DocumentBuilder; |
| 20 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 21 | +import javax.xml.transform.Source; |
| 22 | +import javax.xml.transform.dom.DOMSource; |
| 23 | + |
| 24 | +import org.w3c.dom.Document; |
| 25 | +import org.w3c.dom.Element; |
| 26 | +import org.xml.sax.Attributes; |
| 27 | +import org.xml.sax.ContentHandler; |
| 28 | +import org.xml.sax.SAXException; |
| 29 | +import org.xml.sax.helpers.DefaultHandler; |
| 30 | + |
| 31 | +import org.springframework.ws.server.endpoint.AbstractSaxPayloadEndpoint; |
| 32 | + |
| 33 | +public class ContactCountEndpoint extends AbstractSaxPayloadEndpoint { |
| 34 | + |
| 35 | + private static final String NAMESPACE_URI = "http://www.springframework.org/spring-ws/samples/pox"; |
| 36 | + |
| 37 | + private static final String CONTANT_NAME = "Contact"; |
| 38 | + |
| 39 | + private static final String CONTANT_COUNT_NAME = "ContactCount"; |
| 40 | + |
| 41 | + private DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); |
| 42 | + |
| 43 | + public ContactCountEndpoint() { |
| 44 | + documentBuilderFactory.setNamespaceAware(true); |
| 45 | + } |
| 46 | + |
| 47 | + protected ContentHandler createContentHandler() throws Exception { |
| 48 | + return new ContactCounter(); |
| 49 | + } |
| 50 | + |
| 51 | + protected Source getResponse(ContentHandler contentHandler) throws Exception { |
| 52 | + ContactCounter counter = (ContactCounter) contentHandler; |
| 53 | + |
| 54 | + DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); |
| 55 | + Document response = documentBuilder.newDocument(); |
| 56 | + Element contactCountElement = response.createElementNS(NAMESPACE_URI, CONTANT_COUNT_NAME); |
| 57 | + response.appendChild(contactCountElement); |
| 58 | + contactCountElement.setTextContent(Integer.toString(counter.contactCount)); |
| 59 | + |
| 60 | + return new DOMSource(response); |
| 61 | + } |
| 62 | + |
| 63 | + private static class ContactCounter extends DefaultHandler { |
| 64 | + |
| 65 | + private int contactCount = 0; |
| 66 | + |
| 67 | + public void startElement(String uri, String localName, String qName, Attributes attributes) |
| 68 | + throws SAXException { |
| 69 | + if (NAMESPACE_URI.equals(uri) && CONTANT_NAME.equals(localName)) { |
| 70 | + contactCount++; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + } |
| 76 | +} |
0 commit comments