001: /*
002: * IndexReporter.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.db.report;
013:
014: import java.util.ArrayList;
015: import java.util.Collection;
016: import java.util.LinkedList;
017: import java.util.List;
018: import workbench.db.IndexColumn;
019: import workbench.db.IndexDefinition;
020: import workbench.db.TableIdentifier;
021: import workbench.db.WbConnection;
022: import workbench.util.StrBuffer;
023:
024: /**
025: * Class to retrieve all index definitions for a table and
026: * generate an XML string from that.
027: *
028: * @author support@sql-workbench.net
029: */
030: public class IndexReporter {
031: public static final String TAG_INDEX = "index-def";
032:
033: public static final String TAG_INDEX_NAME = "name";
034: public static final String TAG_INDEX_UNIQUE = "unique";
035: public static final String TAG_INDEX_PK = "primary-key";
036: public static final String TAG_INDEX_TYPE = "type";
037: public static final String TAG_INDEX_EXPR = "index-expression";
038: public static final String TAG_INDEX_COLUMN_LIST = "column-list";
039: public static final String TAG_INDEX_COLUMN_NAME = "column";
040:
041: private Collection<IndexDefinition> indexList;
042: private TagWriter tagWriter = new TagWriter();
043:
044: public IndexReporter(TableIdentifier tbl, WbConnection conn) {
045: indexList = conn.getMetadata().getTableIndexList(tbl);
046: }
047:
048: public IndexReporter(IndexDefinition index) {
049: indexList = new LinkedList<IndexDefinition>();
050: indexList.add(index);
051: }
052:
053: public void appendXml(StrBuffer result, StrBuffer indent) {
054: int numIndex = this .indexList.size();
055: if (numIndex == 0)
056: return;
057: StrBuffer defIndent = new StrBuffer(indent);
058: defIndent.append(" ");
059:
060: for (IndexDefinition index : indexList) {
061: if (index == null)
062: continue;
063: tagWriter.appendOpenTag(result, indent, TAG_INDEX);
064: result.append('\n');
065: tagWriter.appendTag(result, defIndent, TAG_INDEX_NAME,
066: index.getName());
067: tagWriter.appendTag(result, defIndent, TAG_INDEX_EXPR,
068: index.getExpression());
069: tagWriter.appendTag(result, defIndent, TAG_INDEX_UNIQUE,
070: index.isUnique());
071: tagWriter.appendTag(result, defIndent, TAG_INDEX_PK, index
072: .isPrimaryKeyIndex());
073: tagWriter.appendTag(result, defIndent, TAG_INDEX_TYPE,
074: index.getIndexType());
075: List<IndexColumn> columns = index.getColumns();
076: if (columns.size() > 0) {
077: StrBuffer colIndent = new StrBuffer(defIndent);
078: colIndent.append(" ");
079: tagWriter.appendOpenTag(result, defIndent,
080: TAG_INDEX_COLUMN_LIST);
081: result.append('\n');
082: for (IndexColumn col : columns) {
083:
084: List<TagAttribute> attrs = new ArrayList<TagAttribute>(
085: 2);
086: attrs
087: .add(new TagAttribute("name", col
088: .getColumn()));
089:
090: if (col.getDirection() != null) {
091: attrs.add(new TagAttribute("direction", col
092: .getDirection()));
093: }
094: tagWriter.appendOpenTag(result, colIndent,
095: TAG_INDEX_COLUMN_NAME, attrs, false);
096: result.append("/>\n");
097: }
098: tagWriter.appendCloseTag(result, defIndent,
099: TAG_INDEX_COLUMN_LIST);
100: }
101: tagWriter.appendCloseTag(result, indent, TAG_INDEX);
102: }
103: return;
104: }
105:
106: public void setNamespace(String name) {
107: this .tagWriter.setNamespace(name);
108: }
109:
110: public Collection<IndexDefinition> getIndexList() {
111: return this .indexList;
112: }
113:
114: public void done() {
115: }
116: }
|