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.StringReader;
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.Exchange;
import org.apache.camel.InvalidPayloadException;
import org.apache.camel.converter.jaxp.StaxConverter;
import org.apache.camel.spi.NamespaceAware;
import org.apache.camel.support.ExchangeHelper;
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.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 headerName;
protected final String path;
protected char mode;
protected int group;
protected Map nsmap;
public XMLTokenExpressionIterator(String path, char mode) {
this(null, path, mode, 1);
}
public XMLTokenExpressionIterator(String headerName, String path, char mode, int group) {
StringHelper.notEmpty(path, "path");
this.headerName = headerName;
this.path = path;
this.mode = mode;
this.group = group > 1 ? 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(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) {
Reader reader = null;
try {
if (headerName != null) {
String val = exchange.getIn().getHeader(headerName, String.class);
reader = new StringReader(val);
} else {
InputStream in = exchange.getIn().getMandatoryBody(InputStream.class);
String charset = ExchangeHelper.getCharsetName(exchange);
reader = new InputStreamReader(in, charset);
}
return createIterator(reader);
} catch (InvalidPayloadException e) {
exchange.setException(e);
// must close input stream
IOHelper.close(reader);
return null;
} catch (XMLStreamException e) {
exchange.setException(e);
// must close input stream
IOHelper.close(reader);
return null;
} catch (UnsupportedEncodingException e) {
exchange.setException(e);
// must close input stream
IOHelper.close(reader);
return null;
} finally {
if (closeStream) {
IOHelper.close(reader);
}
}
}
static class XMLTokenIterator implements Iterator