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.
/**
* eobjects.org MetaModel
* Copyright (C) 2010 eobjects.org
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.eobjects.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 javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.eobjects.metamodel.MetaModelException;
import org.eobjects.metamodel.MetaModelHelper;
import org.eobjects.metamodel.QueryPostprocessDataContext;
import org.eobjects.metamodel.data.CachingDataSetHeader;
import org.eobjects.metamodel.data.DataSet;
import org.eobjects.metamodel.data.DataSetHeader;
import org.eobjects.metamodel.data.DefaultRow;
import org.eobjects.metamodel.data.InMemoryDataSet;
import org.eobjects.metamodel.data.Row;
import org.eobjects.metamodel.query.FromItem;
import org.eobjects.metamodel.query.JoinType;
import org.eobjects.metamodel.query.Query;
import org.eobjects.metamodel.query.SelectItem;
import org.eobjects.metamodel.schema.Column;
import org.eobjects.metamodel.schema.ColumnType;
import org.eobjects.metamodel.schema.MutableColumn;
import org.eobjects.metamodel.schema.MutableRelationship;
import org.eobjects.metamodel.schema.MutableSchema;
import org.eobjects.metamodel.schema.MutableTable;
import org.eobjects.metamodel.schema.Relationship;
import org.eobjects.metamodel.schema.Schema;
import org.eobjects.metamodel.schema.Table;
import org.eobjects.metamodel.schema.TableType;
import org.eobjects.metamodel.util.FileResource;
import org.eobjects.metamodel.util.ImmutableRef;
import org.eobjects.metamodel.util.NumberComparator;
import org.eobjects.metamodel.util.Ref;
import org.eobjects.metamodel.util.Resource;
import org.eobjects.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
* compliancy with ALL xml formats but also raises a risk that two XML files
* with the same format wont nescesarily 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 Ref _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) {
_autoFlattenTables = autoFlattenTables;
_schemaName = schemaName;
_schema = new MutableSchema(_schemaName);
_inputSourceRef = null;
loadSchema(document);
}
/**
* Creates an XML DataContext strategy based on a file.
*
* @param file
* the file 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 {
_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) {
_inputSourceRef = new ImmutableRef(inputSource);
_schemaName = schemaName;
_autoFlattenTables = autoFlattenTables;
}
public XmlDomDataContext(URL url, boolean autoFlattenTables) throws IllegalArgumentException {
this(new UrlResource(url), autoFlattenTables);
}
private static Ref createInputSourceRef(final Resource resource) {
return new Ref() {
@Override
public InputSource get() {
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, Column[] columns, int maxRows) {
loadSchema();
List