Search the FirstSpirit Knowledge Base
Hallo,
gibt es eine einfache Möglichkeit über die API, die Definition eines Feldes aus einem Formular auszulesen ?
Mittels GomSourceProvider.getGomSource() erhalte ich die komplette Definition, welche ich theoretisch selbst parsen könnte.
Es wäre allerdings schön, das Ganze mit einer API-Funktion erledigen zu können.
Ich habe es nun selbst mit folgender Klasse gelöst:
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import de.espirit.firstspirit.access.store.templatestore.GomSourceProvider;
public class FormParser {
private Document document;
private static DocumentBuilderFactory documentFactory;
private static XPathFactory xPathFactory;
private static TransformerFactory transformerFactory;
static {
documentFactory = DocumentBuilderFactory.newInstance();
xPathFactory = XPathFactory.newInstance();
transformerFactory = TransformerFactory.newInstance();
}
public FormParser(GomSourceProvider gsp) throws ParserConfigurationException, SAXException, IOException {
String source = gsp.getGomSource();
DocumentBuilder builder = documentFactory.newDocumentBuilder();
document = builder.parse(new ByteArrayInputStream(source.getBytes()));
}
public String getFieldDefinition(String field) throws XPathExpressionException, TransformerException {
XPath xPath = xPathFactory.newXPath();
XPathExpression expr = xPath.compile("//*[parent::CMS_MODULE|parent::CMS_GROUP and @name='" + field + "']");
NodeList nl = (NodeList) expr.evaluate(document, XPathConstants.NODESET);
if (nl.getLength() == 0) {
return null;
}
return nodeToString(nl.item(0));
}
private String nodeToString(Node node) throws TransformerException {
StringWriter sw = new StringWriter();
Transformer t = transformerFactory.newTransformer();
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.transform(new DOMSource(node), new StreamResult(sw));
return sw.toString();
}
}
Ich habe es nun selbst mit folgender Klasse gelöst:
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import de.espirit.firstspirit.access.store.templatestore.GomSourceProvider;
public class FormParser {
private Document document;
private static DocumentBuilderFactory documentFactory;
private static XPathFactory xPathFactory;
private static TransformerFactory transformerFactory;
static {
documentFactory = DocumentBuilderFactory.newInstance();
xPathFactory = XPathFactory.newInstance();
transformerFactory = TransformerFactory.newInstance();
}
public FormParser(GomSourceProvider gsp) throws ParserConfigurationException, SAXException, IOException {
String source = gsp.getGomSource();
DocumentBuilder builder = documentFactory.newDocumentBuilder();
document = builder.parse(new ByteArrayInputStream(source.getBytes()));
}
public String getFieldDefinition(String field) throws XPathExpressionException, TransformerException {
XPath xPath = xPathFactory.newXPath();
XPathExpression expr = xPath.compile("//*[parent::CMS_MODULE|parent::CMS_GROUP and @name='" + field + "']");
NodeList nl = (NodeList) expr.evaluate(document, XPathConstants.NODESET);
if (nl.getLength() == 0) {
return null;
}
return nodeToString(nl.item(0));
}
private String nodeToString(Node node) throws TransformerException {
StringWriter sw = new StringWriter();
Transformer t = transformerFactory.newTransformer();
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.transform(new DOMSource(node), new StreamResult(sw));
return sw.toString();
}
}