| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.event.Builder; |
| 12 |
import net.sf.saxon.event.NamespaceReducer; |
| 13 |
import net.sf.saxon.event.Receiver; |
| 14 |
import net.sf.saxon.expr.XPathContext; |
| 15 |
import net.sf.saxon.expr.parser.ExplicitLocation; |
| 16 |
import net.sf.saxon.lib.NamespaceConstant; |
| 17 |
import net.sf.saxon.lib.StandardEntityResolver; |
| 18 |
import net.sf.saxon.om.*; |
| 19 |
import net.sf.saxon.regex.RegexIterator; |
| 20 |
import net.sf.saxon.regex.RegularExpression; |
| 21 |
import net.sf.saxon.trans.XPathException; |
| 22 |
import net.sf.saxon.type.BuiltInAtomicType; |
| 23 |
import net.sf.saxon.type.SchemaType; |
| 24 |
import net.sf.saxon.type.SimpleType; |
| 25 |
import net.sf.saxon.type.Untyped; |
| 26 |
import org.xml.sax.InputSource; |
| 27 |
|
| 28 |
import javax.xml.transform.sax.SAXSource; |
| 29 |
import java.io.InputStream; |
| 30 |
import java.util.ArrayList; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class AnalyzeStringFn extends RegexFunction { |
| 36 |
|
| 37 |
private NodeName resultName; |
| 38 |
private NodeName nonMatchName; |
| 39 |
private NodeName matchName; |
| 40 |
private NodeName groupName; |
| 41 |
private NodeName groupNrName; |
| 42 |
|
| 43 |
private SchemaType resultType = Untyped.getInstance(); |
| 44 |
private SchemaType nonMatchType = Untyped.getInstance(); |
| 45 |
private SchemaType matchType = Untyped.getInstance(); |
| 46 |
private SchemaType groupType = Untyped.getInstance(); |
| 47 |
private SimpleType groupNrType = BuiltInAtomicType.UNTYPED_ATOMIC; |
| 48 |
|
| 49 |
@Override |
| 50 |
protected boolean allowRegexMatchingEmptyString() { |
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
private synchronized void init(Configuration config, boolean schemaAware) throws XPathException { |
| 55 |
resultName = new FingerprintedQName("", NamespaceConstant.FN, "analyze-string-result"); |
| 56 |
nonMatchName = new FingerprintedQName("", NamespaceConstant.FN, "non-match"); |
| 57 |
matchName = new FingerprintedQName("", NamespaceConstant.FN, "match"); |
| 58 |
groupName = new FingerprintedQName("", NamespaceConstant.FN, "group"); |
| 59 |
groupNrName = new NoNamespaceName("nr"); |
| 60 |
|
| 61 |
if (schemaAware) { |
| 62 |
resultType = config.getSchemaType(new StructuredQName("", NamespaceConstant.FN, "analyze-string-result-type")); |
| 63 |
nonMatchType = BuiltInAtomicType.STRING; |
| 64 |
matchType = config.getSchemaType(new StructuredQName("", NamespaceConstant.FN, "match-type")); |
| 65 |
groupType = config.getSchemaType(new StructuredQName("", NamespaceConstant.FN, "group-type")); |
| 66 |
groupNrType = BuiltInAtomicType.POSITIVE_INTEGER; |
| 67 |
if (resultType == null || matchType == null || groupType == null) { |
| 68 |
throw new XPathException("Schema for analyze-string has not been successfully loaded"); |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public NodeInfo call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 83 |
Item inputItem = arguments[0].head(); |
| 84 |
CharSequence input; |
| 85 |
if (inputItem == null) { |
| 86 |
input = ""; |
| 87 |
} else { |
| 88 |
input = inputItem.getStringValueCS(); |
| 89 |
} |
| 90 |
RegularExpression re = getRegularExpression(arguments); |
| 91 |
RegexIterator iter = re.analyze(input); |
| 92 |
|
| 93 |
if (resultName == null) { |
| 94 |
boolean schemaAware = context.getController().getExecutable().isSchemaAware(); |
| 95 |
Configuration config = context.getConfiguration(); |
| 96 |
if (schemaAware && !config.isSchemaAvailable(NamespaceConstant.FN)) { |
| 97 |
StandardEntityResolver resolver = new StandardEntityResolver(); |
| 98 |
resolver.setConfiguration(config); |
| 99 |
InputStream inputStream = Configuration.locateResource("xpath-functions.scm", new ArrayList<>(), new ArrayList<>()); |
| 100 |
if (inputStream == null) { |
| 101 |
throw new XPathException("Failed to load xpath-functions.scm from the classpath"); |
| 102 |
} |
| 103 |
InputSource is = new InputSource(inputStream); |
| 104 |
if (config.isTiming()) { |
| 105 |
config.getLogger().info("Loading schema from resources for: " + NamespaceConstant.FN); |
| 106 |
} |
| 107 |
config.addSchemaSource(new SAXSource(is)); |
| 108 |
} |
| 109 |
init(context.getConfiguration(), schemaAware); |
| 110 |
} |
| 111 |
|
| 112 |
final Builder builder = context.getController().makeBuilder(); |
| 113 |
final Receiver out = new NamespaceReducer(builder); |
| 114 |
builder.setBaseURI(getStaticBaseUriString()); |
| 115 |
out.open(); |
| 116 |
out.startElement(resultName, resultType, ExplicitLocation.UNKNOWN_LOCATION, 0); |
| 117 |
out.startContent(); |
| 118 |
Item item; |
| 119 |
while ((item = iter.next()) != null) { |
| 120 |
if (iter.isMatching()) { |
| 121 |
out.startElement(matchName, matchType, ExplicitLocation.UNKNOWN_LOCATION, 0); |
| 122 |
out.startContent(); |
| 123 |
iter.processMatchingSubstring(new RegexIterator.MatchHandler() { |
| 124 |
public void characters(CharSequence s) throws XPathException { |
| 125 |
out.characters(s, ExplicitLocation.UNKNOWN_LOCATION, 0); |
| 126 |
} |
| 127 |
|
| 128 |
public void onGroupStart(int groupNumber) throws XPathException { |
| 129 |
out.startElement(groupName, groupType, ExplicitLocation.UNKNOWN_LOCATION, 0); |
| 130 |
out.attribute(groupNrName, groupNrType, "" + groupNumber, ExplicitLocation.UNKNOWN_LOCATION, 0); |
| 131 |
out.startContent(); |
| 132 |
} |
| 133 |
|
| 134 |
public void onGroupEnd(int groupNumber) throws XPathException { |
| 135 |
out.endElement(); |
| 136 |
} |
| 137 |
}); |
| 138 |
out.endElement(); |
| 139 |
} else { |
| 140 |
out.startElement(nonMatchName, nonMatchType, ExplicitLocation.UNKNOWN_LOCATION, 0); |
| 141 |
out.startContent(); |
| 142 |
out.characters(item.getStringValueCS(), ExplicitLocation.UNKNOWN_LOCATION, 0); |
| 143 |
out.endElement(); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
out.endElement(); |
| 148 |
out.close(); |
| 149 |
return builder.getCurrentRoot(); |
| 150 |
|
| 151 |
} |
| 152 |
|
| 153 |
} |
| 154 |
|
| 155 |
|