defaultdef.java
来自「数据仓库展示程序」· Java 代码 · 共 1,687 行 · 第 1/5 页
JAVA
1,687 行
templateNamePos = new int[count];
System.arraycopy(poss, 0, templateNamePos, 0, count);
ss[count++] =
template.substring(end, template.length());
templateParts = new String[count];
System.arraycopy(ss, 0, templateParts, 0, count);
return;
}
previousEnd = end;
end = template.indexOf('}', start);
if (end == -1) {
// there was a "${" but not '}' in template
String msg = "Bad template \"" +
template +
"\", it had a \"${\", but no '}'";
msgRecorder.reportError(msg);
return;
}
String name = template.substring(start+2, end);
int pos = convertNameToID(name, msgRecorder);
if (pos == BAD_ID) {
return;
}
poss[count] = pos;
ss[count] = template.substring(previousEnd, start);
start = template.indexOf("${", end);
end++;
count++;
}
} finally {
msgRecorder.popContextName();
}
}
protected abstract String[] getTemplateNames();
private int convertNameToID(final String name,
final mondrian.recorder.MessageRecorder msgRecorder) {
if (name == null) {
String msg = "Template name is null";
msgRecorder.reportError(msg);
return BAD_ID;
}
String[] names = getTemplateNames();
for (int i = 0; i < names.length; i++) {
if (names[i].equals(name)) {
return i;
}
}
String msg = "Bad template name \"" +
name +
"\"";
msgRecorder.reportError(msg);
return BAD_ID;
}
public String getRegex(final String[] names) {
final String space = getSpace();
final String dot = getDot();
final StringBuffer buf = new StringBuffer();
//
// Remember that:
// templateParts.length == templateNamePos.length+1
//
buf.append(templateParts[0]);
for (int i = 0; i < templateNamePos.length; i++) {
String n = names[templateNamePos[i]];
if (space != null) {
n = n.replaceAll(" ", space);
}
if (dot != null) {
n = n.replaceAll("\\.", dot);
}
buf.append(n);
buf.append(templateParts[i+1]);
}
String regex = buf.toString();
if (AggRules.getLogger().isDebugEnabled()) {
StringBuffer bf = new StringBuffer(64);
bf.append(getName());
bf.append(".getRegex:");
bf.append(" for names ");
for (int i = 0; i < names.length; i++) {
bf.append('"');
bf.append(names[i]);
bf.append('"');
if (i+1 < names.length) {
bf.append(", ");
}
}
bf.append(" regex is \"");
bf.append(regex);
bf.append('"');
String msg = bf.toString();
AggRules.getLogger().debug(msg);
}
return regex;
}
protected Recognizer.Matcher getMatcher(final String[] names) {
final String charcase = getCharCase();
final String regex;
int flag = 0;
if (charcase.equals("ignore")) {
// the case of name does not matter
// since the Pattern will be create to ignore case
regex = getRegex(names);
flag = java.util.regex.Pattern.CASE_INSENSITIVE;
} else if (charcase.equals("exact")) {
// the case of name is not changed
// since we are interested in exact case matching
regex = getRegex(names);
} else if (charcase.equals("upper")) {
// convert name to upper case
String[] ucNames = new String[names.length];
for (int i = 0; i < names.length; i++) {
ucNames[i] = names[i].toUpperCase();
}
regex = getRegex(ucNames);
} else {
// lower
// convert name to lower case
String[] lcNames = new String[names.length];
for (int i = 0; i < names.length; i++) {
lcNames[i] = names[i].toLowerCase();
}
regex = getRegex(lcNames);
}
final java.util.regex.Pattern pattern =
java.util.regex.Pattern.compile(regex, flag);
return new Recognizer.Matcher() {
public boolean matches(String name) {
boolean b = pattern.matcher(name).matches();
if (AggRules.getLogger().isDebugEnabled()) {
debug(name);
}
return b;
}
private void debug(String name) {
StringBuffer bf = new StringBuffer(64);
bf.append(Mapper.this.getName());
bf.append(".Machter.matches:");
bf.append(" name ");
bf.append('"');
bf.append(name);
bf.append('"');
bf.append(" patterm \"");
bf.append(pattern.pattern());
bf.append('"');
if ((pattern.flags() &
java.util.regex.Pattern.CASE_INSENSITIVE) != 0) {
bf.append(" case_insensitive");
}
String msg = bf.toString();
AggRules.getLogger().debug(msg);
}
};
}
// END pass-through code block ---
}
/**
* This element is used in a vector of child elements when
* one wishes to have one or more regular expressions associated
* with matching a given string. The parent element must
* initialize Regex object by calling its validate method
* passing in an array of template names.
* The cdata content is a regular expression with embedded
* template names. Each name must be surrounded by "${" and "}".
* Each time this is used for a new set of names, the names
* replace the template names in the regular expression.
* For example, if the charcase="lower", the attribute
* dot="-" (the default dot value is "_"), the template names are:
* "city", "state", and "country"
* and the cdata is:
* .*_${country}_.*_${city}
* Then when the names:
* "San Francisco", "California", and "U.S.A"
* are passed in, the regular expression becomes:
* .*_u-s-a_.*_san_francisco
* Note that a given template name can only appear ONCE in the
* template content, the cdata content. As an example, the
* following cdata template is not supported:
* .*_${country}_.*_${city}_${country}
*/
public static class Regex extends CaseMatcher
{
public Regex()
{
}
public Regex(org.eigenbase.xom.DOMWrapper _def)
throws org.eigenbase.xom.XOMException
{
try {
org.eigenbase.xom.DOMElementParser _parser = new org.eigenbase.xom.DOMElementParser(_def, "", DefaultDef.class);
_parser = _parser;
space = (String)_parser.getAttribute("space", "String", "_", null, false);
dot = (String)_parser.getAttribute("dot", "String", "_", null, false);
id = (String)_parser.getAttribute("id", "String", null, null, true);
charcase = (String)_parser.getAttribute("charcase", "String", "ignore", _charcase_values, false);
enabled = (Boolean)_parser.getAttribute("enabled", "Boolean", "true", null, false);
cdata = _parser.getText();
} catch(org.eigenbase.xom.XOMException _ex) {
throw new org.eigenbase.xom.XOMException("In element '" + getName() + "': " + _ex.getMessage());
}
}
public String space; // attribute default: _
public String dot; // attribute default: _
public String cdata; // All text goes here
public String getName()
{
return "Regex";
}
public void display(java.io.PrintWriter _out, int _indent)
{
_out.println(getName());
displayAttribute(_out, "space", space, _indent+1);
displayAttribute(_out, "dot", dot, _indent+1);
displayAttribute(_out, "id", id, _indent+1);
displayAttribute(_out, "charcase", charcase, _indent+1);
displayAttribute(_out, "enabled", enabled, _indent+1);
displayString(_out, "cdata", cdata, _indent+1);
}
public void displayXML(org.eigenbase.xom.XMLOutput _out, int _indent)
{
_out.beginTag("Regex", new org.eigenbase.xom.XMLAttrVector()
.add("space", space)
.add("dot", dot)
.add("id", id)
.add("charcase", charcase)
.add("enabled", enabled)
);
_out.cdata(cdata);
_out.endTag("Regex");
}
public boolean displayDiff(org.eigenbase.xom.ElementDef _other, java.io.PrintWriter _out, int _indent)
{
boolean _diff = true;
Regex _cother = (Regex)_other;
_diff = _diff && displayAttributeDiff("space", space, _cother.space, _out, _indent+1);
_diff = _diff && displayAttributeDiff("dot", dot, _cother.dot, _out, _indent+1);
_diff = _diff && displayStringDiff("cdata", cdata, _cother.cdata, _out, _indent+1);
return _diff;
}
// BEGIN pass-through code block ---
public String getSpace() {
return space;
}
public String getDot() {
return dot;
}
public String getTemplate() {
return cdata;
}
protected static final int BAD_ID = -1;
protected String[] templateParts;
/**
* This is a one-to-one mapping, each template name can appear
* at most once.
*/
protected int[] templateNamePos;
/**
* It is hoped that no one will need to match more than 50 names
* in a template. Currently, this implementation, requires only 3.
*/
private static final int MAX_SIZE = 50;
public void validate(final AggRules rules,
final String[] templateNames,
final mondrian.recorder.MessageRecorder msgRecorder) {
msgRecorder.pushContextName(getName());
try {
super.validate(rules, msgRecorder);
String[] ss = new String[MAX_SIZE+1];
int[] poss = new int[MAX_SIZE];
String template = getTemplate();
int count = 0;
int end = 0;
int previousEnd = 0;
int start = template.indexOf("${", end);
while (count < MAX_SIZE) {
if (start == -1) {
if (count == 0) {
// there are no ${} in template which
// is an error
String msg = "Bad template \"" +
template +
"\", no ${} entries";
msgRecorder.reportError(msg);
return;
}
// its OK, there are "count" ${}
templateNamePos = new int[count];
System.arraycopy(poss, 0, templateNamePos, 0, count);
ss[count++] =
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?