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.
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.metamodel.xml;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.metamodel.MetaModelException;
import org.apache.metamodel.QueryPostprocessDataContext;
import org.apache.metamodel.data.CachingDataSetHeader;
import org.apache.metamodel.data.DataSet;
import org.apache.metamodel.data.DataSetHeader;
import org.apache.metamodel.data.DefaultRow;
import org.apache.metamodel.data.InMemoryDataSet;
import org.apache.metamodel.data.Row;
import org.apache.metamodel.query.FromItem;
import org.apache.metamodel.query.JoinType;
import org.apache.metamodel.query.Query;
import org.apache.metamodel.query.SelectItem;
import org.apache.metamodel.schema.Column;
import org.apache.metamodel.schema.ColumnType;
import org.apache.metamodel.schema.MutableColumn;
import org.apache.metamodel.schema.MutableRelationship;
import org.apache.metamodel.schema.MutableSchema;
import org.apache.metamodel.schema.MutableTable;
import org.apache.metamodel.schema.Relationship;
import org.apache.metamodel.schema.Schema;
import org.apache.metamodel.schema.Table;
import org.apache.metamodel.schema.TableType;
import org.apache.metamodel.util.FileResource;
import org.apache.metamodel.util.ImmutableRef;
import org.apache.metamodel.util.NumberComparator;
import org.apache.metamodel.util.Resource;
import org.apache.metamodel.util.UrlResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Attr;
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.Text;
import org.xml.sax.InputSource;
/**
* A DataContext strategy that reads XML content and maps it to a table-based
* model similar to the rest of MetaModel. Tables are created by examining the
* data in the XML file, NOT by reading XML Schemas (xsd/dtd's). This enables
* compliance with ALL xml formats but also raises a risk that two XML files
* with the same format wont necessarily yield the same table model if some
* optional attributes or tags are omitted in one of the files.
*
* The parsing method applied in this datacontext is DOM based, which means that
* at upon parsing (only a single point in time), the whole file will be read
* and it's tree structure kept in memory. Therefore this DataContext is NOT
* appropriate for large XML files (10's, 100's or 1000's of megabytes).
*
* @see XmlSaxDataContext
*/
public class XmlDomDataContext extends QueryPostprocessDataContext {
private static final Logger logger = LoggerFactory.getLogger(XmlDomDataContext.class);
public static final String NATIVE_TYPE_PRIMARY_KEY = "Auto-generated primary key";
public static final String NATIVE_TYPE_FOREIGN_KEY = "Auto-generated foreign key";
public static final String NATIVE_TYPE_ATTRIBUTE = "XML Attribute";
public static final String NATIVE_TYPE_TEXT = "XML Text";
private static final String TEXT_CONTENT_TEMP_SUFFIX = "_metamodel_text_content";
private final Supplier _inputSourceRef;
private final Map> _tableData = new HashMap>();;
private final String _schemaName;
private MutableSchema _schema;
private boolean _autoFlattenTables;
/**
* Creates an XML DataContext strategy based on an already parsed Document.
*
* @param schemaName
* @param document
* @param autoFlattenTables
*/
public XmlDomDataContext(String schemaName, Document document, boolean autoFlattenTables) {
super(false);
_autoFlattenTables = autoFlattenTables;
_schemaName = schemaName;
_schema = new MutableSchema(_schemaName);
_inputSourceRef = null;
loadSchema(document);
}
/**
* Creates an XML DataContext strategy based on a file.
*
* @param resource
* the resource to parse
* @param autoFlattenTables
* a parameter indicating whether or not tags with only text
* content or a single attribute should be flattened with it's
* parent table
*
* @throws IllegalArgumentException
* if the file does not exist
*/
public XmlDomDataContext(Resource resource, boolean autoFlattenTables) throws IllegalArgumentException {
super(false);
_inputSourceRef = createInputSourceRef(resource);
_schemaName = resource.getName();
_autoFlattenTables = autoFlattenTables;
}
public XmlDomDataContext(File file, boolean autoFlattenTables) {
this(new FileResource(file), autoFlattenTables);
}
public XmlDomDataContext(InputSource inputSource, String schemaName, boolean autoFlattenTables) {
super(false);
_inputSourceRef = new ImmutableRef(inputSource);
_schemaName = schemaName;
_autoFlattenTables = autoFlattenTables;
}
public XmlDomDataContext(URL url, boolean autoFlattenTables) throws IllegalArgumentException {
this(new UrlResource(url), autoFlattenTables);
}
private static Supplier createInputSourceRef(final Resource resource) {
return () -> {
final InputStream in = resource.read();
return new InputSource(in);
};
}
/**
* Creates an XML DataContext strategy based on a file.
*
* @param file
* the file to parse
*/
public XmlDomDataContext(File file) {
this(file, true);
}
public boolean isAutoFlattenTables() {
return _autoFlattenTables;
}
public void setAutoFlattenTables(boolean autoFlattenTables) {
_autoFlattenTables = autoFlattenTables;
}
@Override
public DataSet materializeMainSchemaTable(Table table, List columnList, int maxRows) {
loadSchema();
List