Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.devonfw.cobigen.xmlplugin.inputreader;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.traversal.DocumentTraversal;
import org.w3c.dom.traversal.NodeFilter;
import org.w3c.dom.traversal.TreeWalker;
import org.xml.sax.SAXException;
import com.devonfw.cobigen.api.exception.InputReaderException;
import com.devonfw.cobigen.api.extension.InputReader;
/** {@link InputReader} for XML files. */
public class XmlInputReader implements InputReader {
/** Logger instance. */
private static final Logger LOG = LoggerFactory.getLogger(XmlInputReader.class);
@Override
public boolean isValidInput(Object input) {
if (input instanceof Document || input instanceof Path && Files.isRegularFile((Path) input)) {
return true;
} else if (input instanceof Node[] && ((Node[]) input).length == 2) {
Node[] inputArr = (Node[]) input;
return inputArr[0] instanceof Document;
} else {
return false;
}
}
@Override
public Map createModel(Object input) {
if (isValidInput(input)) {
if (input instanceof Document) {
Map model = new HashMap<>();
fillModel((Document) input, model, ModelConstant.ROOT_DOC);
return model;
} else if (input instanceof Node[]) {
Map model = new HashMap<>();
// Document newXmlDocument = createSubDoc(nextNode);
fillModel((Document) ((Node[]) input)[0], model, ModelConstant.ROOT_DOC);
fillModel(((Node[]) input)[1], model, ModelConstant.ROOT_ELEMDOC);
return model;
} else {
throw new IllegalArgumentException(
"XmlInputReader::createModel(Object) called with invalid parameter value. This is a bug.");
}
} else {
return null;
}
}
/**
* Adds the given document to the given model. First, the string based model is derived and added by the
* root node's key. Second, the document itself is added under the given XPath root key.
* @param doc
* the document to be processed
* @param model
* the model to be enriched
* @param xpathRootKey
* key of the document in the model to be accessible for xpath expressions
*/
private void fillModel(Document doc, Map model, String xpathRootKey) {
Element rootElement = doc.getDocumentElement();
// custom extracted model for more convenient navigation in the template languages
model.put(rootElement.getNodeName(), deriveSubModel(rootElement));
// complete access to allow xpath
model.put(xpathRootKey, doc);
}
/** @see #fillModel(Document, Map, String) */
@SuppressWarnings("javadoc")
private void fillModel(Node rootNode, Map model, String xpathRootKey) {
// custom extracted model for more convenient navigation in the template languages
model.put(rootNode.getNodeName(), deriveSubModel((Element) rootNode));
// complete access to allow xpath
model.put(xpathRootKey, rootNode);
}
/**
* {@inheritDoc}
* Splits an XMI Document into multiple sub-documents, one per each found class. Returns a {@link List} of
* DocumentImpl.
*/
@Override
public List