All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.metamodel.xml.XmlSaxTableDef Maven / Gradle / Ivy

The newest version!
/**
 * 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.Serializable;
import java.util.Arrays;
import java.util.Collection;

import org.apache.metamodel.schema.Table;

/**
 * Defines a table layout for {@link XmlSaxDataContext}. This class is used as
 * an instruction set of xpath expressions for the datacontext to create
 * {@link Table}s.
 * 
 * These types of expressions are allowed in the {@link XmlSaxTableDef}:
 * 
 * 
    *
  • Element selectors: "/path/to/element"
  • *
  • Attribute selectors: "/path/to/element@attribute"
  • *
  • Element index selectors: "index(/path/to)" (can be used to make * cross-references to parent tags)
  • *
* * If, for example, this is your XML document: * *
 * <root>
 * 	<organization type="company">
 * 		<name>Company A</name>
 * 		<employees>
 * 			<employee>
 * 				<name>John Doe</name>
 * 				<gender>M</gender>
 * 			</employee>
 * 			<employee>
 * 				<name>Jane Doe</name>
 * 				<gender>F</gender>
 * 			</employee>
 * 		</employees>
 * 	</organization>
 * 	<organization type="government">
 * 		<name>Company B</name>
 * 		<employees>
 * 			<employee>
 * 				<name>Susan</name>
 * 				<gender>F</gender>
 * 			</employee>
 * 		</employees>
 * 	</organization>
 * </root>
 * 
* * Then if you wanted to extract information about organizations, these xpaths * could work: *
    *
  • Organization row scope: "/root/organization"
  • *
  • Organization name: "/root/organization/name"
  • *
  • Organization type: "/root/organization@type"
  • *
* * Or if you wanted to extract information about employees: *
    *
  • Employee row scope: "/root/organization/employees/employee"
  • *
  • Employee name: "/root/organization/employees/employee/name"
  • *
  • Employee gender: "/root/organization/employees/employee/gender"
  • *
  • Employee organization index: "index(/root/organization)"
  • *
*/ public final class XmlSaxTableDef implements Serializable { private static final long serialVersionUID = 1L; private final String _rowXpath; private final String[] _valueXpaths; /** * Constructs a {@link XmlSaxTableDef} based on an xpath expression for the * row scope and an array of xpath expressions for the individual values * (columns) within a row. * * @param rowXpath * an xpath expression for the scope of a record, eg. * /companies/company/employee * @param valueXpaths * an array of xpath expressions for the individual values * (columns) of a row. eg: [/companies/company/employee/name, * /companies/company/employee/gender, index(/companies/company)] */ public XmlSaxTableDef(String rowXpath, String[] valueXpaths) { _rowXpath = rowXpath; _valueXpaths = valueXpaths; } public XmlSaxTableDef(String rowXpath, Collection valueXpaths) { this(rowXpath, valueXpaths.toArray(new String[valueXpaths.size()])); } public String getRowXpath() { return _rowXpath; } public String[] getValueXpaths() { return _valueXpaths; } @Override public int hashCode() { return _rowXpath.hashCode(); } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; XmlSaxTableDef other = (XmlSaxTableDef) obj; if (_rowXpath == null) { if (other._rowXpath != null) return false; } else if (!_rowXpath.equals(other._rowXpath)) return false; if (!Arrays.equals(_valueXpaths, other._valueXpaths)) return false; return true; } @Override public String toString() { return "XmlSaxTableDef[rowXpath=" + _rowXpath + ",valueXpaths=" + Arrays.toString(_valueXpaths) + "]"; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy