|
| 1 | +/* |
| 2 | + * Hibernate Validator, declare and validate application constraints |
| 3 | + * |
| 4 | + * License: Apache License, Version 2.0 |
| 5 | + * See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>. |
| 6 | + */ |
| 7 | +package org.hibernate.validator.internal.xml; |
| 8 | + |
| 9 | +import java.util.Optional; |
| 10 | + |
| 11 | +import javax.xml.namespace.QName; |
| 12 | +import javax.xml.stream.XMLEventReader; |
| 13 | +import javax.xml.stream.XMLStreamException; |
| 14 | +import javax.xml.stream.events.Attribute; |
| 15 | +import javax.xml.stream.events.StartElement; |
| 16 | +import javax.xml.stream.events.XMLEvent; |
| 17 | + |
| 18 | +/** |
| 19 | + * Other Stax xml builders should extend from this one. |
| 20 | + * Provides some common functionality like reading an attribute value |
| 21 | + * or value of a simple tag. |
| 22 | + * |
| 23 | + * @author Marko Bekhta |
| 24 | + */ |
| 25 | +public abstract class AbstractStaxBuilder { |
| 26 | + |
| 27 | + protected abstract String getAcceptableQName(); |
| 28 | + |
| 29 | + /** |
| 30 | + * Checks if the given {@link XMLEvent} is a {@link StartElement} and if the |
| 31 | + * corresponding xml tag can be processed based on a tag name. |
| 32 | + * |
| 33 | + * @param xmlEvent an event to check |
| 34 | + * |
| 35 | + * @return {@code true} if corresponding event can be processed by current builder, |
| 36 | + * {@code false} otherwise |
| 37 | + */ |
| 38 | + protected boolean accept(XMLEvent xmlEvent) { |
| 39 | + return xmlEvent.isStartElement() && xmlEvent.asStartElement().getName().getLocalPart().equals( getAcceptableQName() ); |
| 40 | + } |
| 41 | + |
| 42 | + public boolean process(XMLEventReader xmlEventReader, XMLEvent xmlEvent) { |
| 43 | + if ( accept( xmlEvent ) ) { |
| 44 | + try { |
| 45 | + add( xmlEventReader, xmlEvent ); |
| 46 | + } |
| 47 | + catch (XMLStreamException e) { |
| 48 | + throw new IllegalStateException( e ); |
| 49 | + } |
| 50 | + return true; |
| 51 | + } |
| 52 | + return false; |
| 53 | + } |
| 54 | + |
| 55 | + protected abstract void add(XMLEventReader xmlEventReader, XMLEvent xmlEvent) throws XMLStreamException; |
| 56 | + |
| 57 | + /** |
| 58 | + * Reads a value between a simple tag element. In case of a {@code <someTag>some-value</someTag>} will |
| 59 | + * return {@code some-value} as a string. |
| 60 | + * |
| 61 | + * @param xmlEventReader a current {@link XMLEventReader} |
| 62 | + * |
| 63 | + * @return a value of a current xml tag as a string |
| 64 | + */ |
| 65 | + protected String readSingleElement(XMLEventReader xmlEventReader) throws XMLStreamException { |
| 66 | + // trimming the string value as it might contain leading/trailing spaces or \n |
| 67 | + XMLEvent xmlEvent = xmlEventReader.nextEvent(); |
| 68 | + StringBuilder stringBuilder = new StringBuilder( xmlEvent.asCharacters().getData() ); |
| 69 | + while ( xmlEventReader.peek().isCharacters() ) { |
| 70 | + xmlEvent = xmlEventReader.nextEvent(); |
| 71 | + stringBuilder.append( xmlEvent.asCharacters().getData() ); |
| 72 | + } |
| 73 | + return stringBuilder.toString().trim(); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Reads a value of an attribute of a given element. |
| 78 | + * |
| 79 | + * @param startElement an element to get an attribute from |
| 80 | + * @param qName a {@link QName} of an attribute to read |
| 81 | + * |
| 82 | + * @return a value of an attribute if it is present, {@link Optional#empty()} otherwise |
| 83 | + */ |
| 84 | + protected Optional<String> readAttribute(StartElement startElement, QName qName) { |
| 85 | + Attribute attribute = startElement.getAttributeByName( qName ); |
| 86 | + return Optional.ofNullable( attribute ).map( Attribute::getValue ); |
| 87 | + } |
| 88 | +} |
0 commit comments