001: /*
002: * ModifierArguments.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.sql.wbcommands;
013:
014: import java.util.List;
015: import workbench.db.ColumnIdentifier;
016: import workbench.db.importer.modifier.ImportValueModifier;
017: import workbench.db.importer.modifier.RegexModifier;
018: import workbench.db.importer.modifier.SubstringModifier;
019: import workbench.db.importer.modifier.ValueFilter;
020: import workbench.util.ArgumentParser;
021: import workbench.util.StringUtil;
022:
023: /**
024: *
025: * @author support@sql-workbench.net
026: */
027: public class ModifierArguments {
028: public static final String ARG_SUBSTRING = "colSubstring";
029: public static final String ARG_REGEX = "colReplacement";
030: public static final String ARG_MAXLENGTH = "maxLength";
031:
032: private SubstringModifier substring = new SubstringModifier();
033: private RegexModifier regex = new RegexModifier();
034:
035: public static final void addArguments(ArgumentParser cmdLine) {
036: cmdLine.addArgument(ARG_REGEX);
037: cmdLine.addArgument(ARG_SUBSTRING);
038: cmdLine.addArgument(ARG_MAXLENGTH);
039: }
040:
041: public ModifierArguments(ArgumentParser cmdLine)
042: throws NumberFormatException {
043: String value = cmdLine.getValue(ARG_MAXLENGTH);
044: parseSubstring(value);
045: value = cmdLine.getValue(ARG_SUBSTRING);
046: parseSubstring(value);
047: value = cmdLine.getValue(ARG_REGEX);
048: parseRegex(value);
049: }
050:
051: private void parseRegex(String parameterValue) {
052: if (parameterValue == null)
053: return;
054:
055: List<String> entries = StringUtil.stringToList(parameterValue,
056: ",", true, true, false);
057: if (entries.size() == 0)
058: return;
059:
060: for (String entry : entries) {
061: String[] parts = entry.split("=");
062: if (parts.length == 2 && parts[0] != null
063: && parts[1] != null) {
064: ColumnIdentifier col = new ColumnIdentifier(parts[0]);
065: String[] limits = parts[1].split(":");
066: String expression = null;
067: String replacement = "";
068:
069: if (limits.length == 1) {
070: expression = limits[0].trim();
071: } else if (limits.length == 2) {
072: expression = limits[0].trim();
073: replacement = limits[1].trim();
074: }
075: regex.addDefinition(col, expression, replacement);
076: }
077: }
078: }
079:
080: /**
081: * Parses a parameter value for column substring definitions.
082: * e.g. description=0:5,firstname=5:10
083: * the two values define the parameters for String.substring(int, int)
084: *
085: * If only one value is supplied, it is assumed to be a maxlength,
086: * so description=5 is equivalent to description=0:5
087: */
088: private void parseSubstring(String parameterValue)
089: throws NumberFormatException {
090: if (parameterValue == null)
091: return;
092:
093: List<String> entries = StringUtil.stringToList(parameterValue,
094: ",", true, true, false);
095: if (entries.size() == 0)
096: return;
097:
098: for (String entry : entries) {
099: String[] parts = entry.split("=");
100: if (parts.length == 2 && parts[0] != null
101: && parts[1] != null) {
102: ColumnIdentifier col = new ColumnIdentifier(parts[0]);
103: String[] limits = parts[1].split(":");
104: int start = 0;
105: int end = 0;
106:
107: if (limits.length == 1) {
108: end = Integer.valueOf(limits[0].trim()).intValue();
109: } else if (limits.length == 2) {
110: start = Integer.valueOf(limits[0].trim())
111: .intValue();
112: end = Integer.valueOf(limits[1].trim()).intValue();
113: }
114: substring.addDefinition(col, start, end);
115: }
116: }
117: }
118:
119: /**
120: * For testing purposes to access the initialized modifier
121: */
122: SubstringModifier getSubstringModifier() {
123: return substring;
124: }
125:
126: /**
127: * For testing purposes to access the initialized modifier
128: */
129: RegexModifier getRegexModifier() {
130: return regex;
131: }
132:
133: /**
134: * Returns a combined modifier with substring and regex modifications.
135: * the substring modifier will be applied before the regex modifier
136: * during import.
137: *
138: * @return an ImportValueModifier that applies substring and regex modifications.
139: */
140: public ImportValueModifier getModifier() {
141: ValueFilter filter = new ValueFilter();
142: if (substring.getSize() > 0)
143: filter.addColumnModifier(substring);
144: if (regex.getSize() > 0)
145: filter.addColumnModifier(regex);
146: if (filter.getSize() > 0) {
147: return filter;
148: }
149: return null;
150: }
151:
152: }
|