001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * Created Sep 15, 2005
014: * @author wseyler
015: */
016:
017: package org.pentaho.data.connection.xquery;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022: import net.sf.saxon.om.Axis;
023: import net.sf.saxon.om.AxisIterator;
024: import net.sf.saxon.tinytree.TinyNodeImpl;
025: import net.sf.saxon.type.Type;
026:
027: import org.pentaho.commons.connection.AbstractPentahoMetaData;
028:
029: /**
030: * @author wseyler
031: *
032: * TODO To change the template for this generated type comment go to Window -
033: * Preferences - Java - Code Style - Code Templates
034: */
035: public class XQMetaData extends AbstractPentahoMetaData {
036: public static final String DEFAULT_COLUMN_NAME = "Default Column"; //$NON-NLS-1$
037:
038: private Object[][] columnHeaders;
039:
040: private Object[][] rowHeaders;
041:
042: int rowCount = 0;
043:
044: public XQMetaData(Iterator iter) {
045: List headers = new ArrayList();
046: while (iter.hasNext()) {
047: rowCount++;
048: Object obj = iter.next();
049: if (obj instanceof TinyNodeImpl) {
050: boolean processedChildren = false;
051: AxisIterator aIter = ((TinyNodeImpl) obj)
052: .iterateAxis(Axis.DESCENDANT);
053: Object descendent = aIter.next();
054: while (descendent != null) {
055: if (descendent instanceof TinyNodeImpl
056: && ((TinyNodeImpl) descendent)
057: .getNodeKind() == Type.ELEMENT) {
058: TinyNodeImpl descNode = (TinyNodeImpl) descendent;
059: processedChildren = true;
060: if (!headers
061: .contains(descNode.getDisplayName())) {
062: headers.add(descNode.getDisplayName());
063: }
064: }
065: descendent = aIter.next();
066: }
067: if (!processedChildren) {
068: Object value = ((TinyNodeImpl) obj)
069: .getDisplayName();
070: if (!headers.contains(value)) {
071: headers.add(value);
072: }
073: }
074: }
075: }
076: if (headers.size() > 0) {
077: columnHeaders = new Object[1][headers.size()];
078: Iterator headerIter = headers.iterator();
079: int i = 0;
080: while (headerIter.hasNext()) {
081: columnHeaders[0][i] = headerIter.next();
082: i++;
083: }
084: } else {
085: columnHeaders = new Object[1][1];
086: columnHeaders[0][0] = DEFAULT_COLUMN_NAME;
087: }
088:
089: }
090:
091: public int getRowCount() {
092: return rowCount;
093: }
094:
095: /*
096: * (non-Javadoc)
097: *
098: * @see org.pentaho.connection.IPentahoMetaData#getColumnHeaders()
099: */
100: public Object[][] getColumnHeaders() {
101: return columnHeaders;
102: }
103:
104: /*
105: * (non-Javadoc)
106: *
107: * @see org.pentaho.connection.IPentahoMetaData#getRowHeaders()
108: */
109: public Object[][] getRowHeaders() {
110: return rowHeaders;
111: }
112: }
|