gdv.xport.satz.xml.FeldXml Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gdv-xport-lib Show documentation
Show all versions of gdv-xport-lib Show documentation
gdv-xport-lib ist die Java-Bibliothek fuer den Umgang mit dem GDV-Format.
Sie erleichtert den Export und Export dieses Datenformats.
/*
* Copyright (c) 2014 by Oli B.
*
* 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 orimplied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* (c)reated 11.08.2014 by Oli B. ([email protected])
*/
package gdv.xport.satz.xml;
import de.jfachwert.Text;
import gdv.xport.feld.*;
import gdv.xport.util.XmlHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.StartElement;
import java.util.Properties;
/**
* Im Gegensatz zur {@link Feld}-Klasse kommen hier die einzelnen Werte als
* XML-Strem rein.
*
* @author oliver ([email protected])
* @since 1.0 (11.08.2014)
*/
public final class FeldXml extends Feld {
private static final Logger LOG = LogManager.getLogger(FeldXml.class);
private final String id;
private final Datentyp datentyp;
private final int nachkommastellen;
/**
* Instantiiert eine Objekt mit den Werten, die ueber den uebergebenen
* Parser gelesen werden.
*
* @param parser the parser
* @throws XMLStreamException the XML stream exception
*/
public FeldXml(final XMLEventReader parser) throws XMLStreamException {
this(parser, XmlHelper.getNextStartElement(parser));
}
/**
* Instantiiert eine Objekt mit den Werten, die ueber den uebergebenen
* Parser gelesen werden.
*
* @param parser the parser
* @param element das Start-Element <feldreferenz referenz=... >
* @throws XMLStreamException the XML stream exception
*/
public FeldXml(final XMLEventReader parser, final StartElement element) throws XMLStreamException {
this(parse(parser, element));
}
private FeldXml(final Properties props) {
super(new Bezeichner(props), 0, toInhalt(props), Align.LEFT);
this.id = props.getProperty("ID");
this.datentyp = Datentyp.asValue(props.getProperty("datentyp"));
this.nachkommastellen = Integer.parseInt(props.getProperty("nachkommastellen", "0"));
LOG.debug("{} created.", this);
}
private static String toInhalt(Properties props) {
int l = Integer.parseInt(props.getProperty("bytes", "1"));
String s = props.getProperty("auspraegung", " ");
return StringUtils.rightPad(s, l);
}
private static Properties parse(final XMLEventReader parser, final StartElement element) throws XMLStreamException {
String xid = element.getAttributeByName(new QName("referenz")).getValue();
LOG.trace("Parsing