001: /*
002: * ReportSequence.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.io.IOException;
015: import java.io.Writer;
016: import java.util.Iterator;
017: import workbench.db.SequenceDefinition;
018: import workbench.util.StrBuffer;
019:
020: /**
021: *
022: * @author support@sql-workbench.net
023: */
024: public class ReportSequence {
025: public static final String TAG_SEQ_DEF = "sequence-def";
026: public static final String TAG_SEQ_NAME = "sequence-name";
027: public static final String TAG_SEQ_CATALOG = "sequence-catalog";
028: public static final String TAG_SEQ_SCHEMA = "sequence-schema";
029: public static final String TAG_SEQ_COMMENT = "sequence-comment";
030: public static final String TAG_SEQ_SOURCE = "sequence-source";
031: public static final String TAG_SEQ_PROPS = "sequence-properties";
032: public static final String TAG_SEQ_PROPERTY = "property";
033: public static final String TAG_SEQ_PROP_NAME = "name";
034: public static final String TAG_SEQ_PROP_VALUE = "value";
035:
036: private SequenceDefinition sequence;
037: private TagWriter tagWriter = new TagWriter();
038: private String schemaNameToUse = null;
039:
040: public ReportSequence(SequenceDefinition def, String nspace) {
041: this .sequence = def;
042: this .tagWriter.setNamespace(nspace);
043: }
044:
045: public SequenceDefinition getSequence() {
046: return this .sequence;
047: }
048:
049: public void writeXml(Writer out) throws IOException {
050: StrBuffer line = this .getXml();
051: line.writeTo(out);
052: }
053:
054: public void setSchemaNameToUse(String schema) {
055: this .schemaNameToUse = schema;
056: }
057:
058: public StrBuffer getXml() {
059: return getXml(new StrBuffer(" "));
060: }
061:
062: /**
063: * Return an XML representation of this view information.
064: * The columns will be listed alphabetically not in the order
065: * they were retrieved from the database.
066: */
067: public StrBuffer getXml(StrBuffer indent) {
068: StrBuffer line = new StrBuffer(500);
069: StrBuffer colindent = new StrBuffer(indent);
070: colindent.append(indent);
071:
072: tagWriter.appendOpenTag(line, indent, TAG_SEQ_DEF, "name",
073: this .sequence.getSequenceName());
074: line.append('\n');
075: tagWriter.appendTag(line, colindent, TAG_SEQ_SCHEMA,
076: (this .schemaNameToUse == null ? this .sequence
077: .getSequenceOwner() : this .schemaNameToUse));
078: tagWriter.appendTag(line, colindent, TAG_SEQ_NAME,
079: this .sequence.getSequenceName());
080:
081: writeSequenceProperties(line, colindent);
082: writeSourceTag(tagWriter, line, colindent, sequence.getSource());
083:
084: tagWriter.appendCloseTag(line, indent, TAG_SEQ_DEF);
085: return line;
086: }
087:
088: public void writeSourceTag(TagWriter tagWriter, StrBuffer target,
089: StrBuffer indent, CharSequence source) {
090: if (source == null)
091: return;
092: tagWriter.appendOpenTag(target, indent, TAG_SEQ_SOURCE);
093: target.append(TagWriter.CDATA_START);
094: target.append(source);
095: target.append(TagWriter.CDATA_END);
096: target.append('\n');
097: tagWriter.appendCloseTag(target, indent, TAG_SEQ_SOURCE);
098: }
099:
100: protected void writeSequenceProperties(StrBuffer toAppend,
101: StrBuffer indent) {
102: StrBuffer myindent = new StrBuffer(indent);
103: myindent.append(" ");
104: StrBuffer propindent = new StrBuffer(myindent);
105: propindent.append(" ");
106:
107: tagWriter.appendOpenTag(toAppend, indent, TAG_SEQ_PROPS);
108: toAppend.append('\n');
109: Iterator<String> itr = this .sequence.getProperties();
110: while (itr.hasNext()) {
111: String propName = itr.next();
112: Object value = this .sequence.getSequenceProperty(propName);
113: TagAttribute name = new TagAttribute("name", propName);
114: TagAttribute v = new TagAttribute("value",
115: (value == null ? "" : value.toString()));
116: tagWriter.appendOpenTag(toAppend, myindent,
117: TAG_SEQ_PROPERTY, false, name, v);
118: toAppend.append("/>\n");
119: }
120: tagWriter.appendCloseTag(toAppend, indent, TAG_SEQ_PROPS);
121: }
122:
123: }
|