Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.language.xtokenizer;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Expression;
import org.apache.camel.InvalidPayloadException;
import org.apache.camel.converter.jaxp.StaxConverter;
import org.apache.camel.spi.NamespaceAware;
import org.apache.camel.support.ExpressionAdapter;
import org.apache.camel.util.IOHelper;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.StringHelper;
import org.apache.camel.xml.io.util.XmlStreamReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* An {@link org.apache.camel.language.xtokenizer.XMLTokenizeLanguage} based iterator.
*/
public class XMLTokenExpressionIterator extends ExpressionAdapter implements NamespaceAware {
protected final String path;
protected char mode;
protected int group;
protected Expression source;
protected Map nsmap;
public XMLTokenExpressionIterator(String path, char mode) {
this(null, path, mode);
}
public XMLTokenExpressionIterator(Expression source, String path, char mode) {
this.source = source;
this.path = path;
this.mode = mode;
}
@Override
public void init(CamelContext context) {
super.init(context);
// group must be 1 or higher
this.group = Math.max(group, 1);
}
@Override
public void setNamespaces(Map nsmap) {
this.nsmap = nsmap;
}
@Override
public Map getNamespaces() {
return nsmap;
}
public void setMode(char mode) {
this.mode = mode;
}
public void setMode(String mode) {
this.mode = mode != null ? mode.charAt(0) : 0;
}
public int getGroup() {
return group;
}
public void setGroup(int group) {
this.group = group;
}
protected Iterator> createIterator(InputStream in, String charset)
throws XMLStreamException, UnsupportedEncodingException {
return createIterator(new InputStreamReader(in, charset));
}
protected Iterator> createIterator(InputStream in)
throws XMLStreamException, IOException {
return createIterator(new XmlStreamReader(in));
}
protected Iterator> createIterator(Reader in) throws XMLStreamException {
return new XMLTokenIterator(path, nsmap, mode, group, in);
}
@Override
public boolean matches(Exchange exchange) {
// as a predicate we must close the stream, as we do not return an iterator that can be used
// afterwards to iterate the input stream
Object value = doEvaluate(exchange, true);
return ObjectHelper.evaluateValuePredicate(value);
}
@Override
public Object evaluate(Exchange exchange) {
// as we return an iterator to access the input stream, we should not close it
return doEvaluate(exchange, false);
}
/**
* Strategy to evaluate the exchange
*
* @param exchange the exchange
* @param closeStream whether to close the stream before returning from this method.
* @return the evaluated value
*/
protected Object doEvaluate(Exchange exchange, boolean closeStream) {
InputStream in = null;
Reader reader = null;
try {
if (source != null) {
in = source.evaluate(exchange, InputStream.class);
} else {
in = exchange.getIn().getBody(InputStream.class);
}
if (in == null) {
throw new InvalidPayloadException(exchange, InputStream.class);
}
// use xml stream reader which is capable of handling reading the xml stream
// according to charset
reader = new XmlStreamReader(in);
return createIterator(reader);
} catch (Exception e) {
exchange.setException(e);
// must close input stream
IOHelper.close(in, reader);
return null;
} finally {
if (closeStream) {
IOHelper.close(in, reader);
}
}
}
static class XMLTokenIterator implements Iterator