use of javax.xml.stream.XMLStreamException in project camel by apache.
the class XMLStreamReaderInputStream method ensureBuffering.
private int ensureBuffering(int size) throws IOException {
if (size < bpos) {
return bpos;
}
// refill the buffer as more buffering is requested than the current buffer status
try {
// very first event
if (XMLStreamConstants.START_DOCUMENT == reader.getEventType()) {
writer.writeStartDocument(charset, "1.0");
}
if (chunk.size() < buffer.length) {
while (reader.hasNext()) {
int code = reader.next();
switch(code) {
case XMLStreamConstants.END_DOCUMENT:
writer.writeEndDocument();
break;
case XMLStreamConstants.START_ELEMENT:
QName qname = reader.getName();
writer.writeStartElement(qname.getPrefix(), qname.getLocalPart(), qname.getNamespaceURI());
for (int i = 0; i < reader.getAttributeCount(); i++) {
String namespaceUri = reader.getAttributeNamespace(i);
writer.writeAttribute(reader.getAttributePrefix(i), namespaceUri == null ? "" : namespaceUri, reader.getAttributeLocalName(i), reader.getAttributeValue(i));
}
for (int i = 0; i < reader.getNamespaceCount(); i++) {
writer.writeNamespace(reader.getNamespacePrefix(i), reader.getNamespaceURI(i));
}
break;
case XMLStreamConstants.END_ELEMENT:
writer.writeEndElement();
break;
case XMLStreamConstants.CHARACTERS:
writer.writeCharacters(reader.getText());
break;
case XMLStreamConstants.COMMENT:
writer.writeComment(reader.getText());
break;
case XMLStreamConstants.CDATA:
writer.writeCData(reader.getText());
break;
default:
break;
}
// check if the chunk is full
final int csize = buffer.length - bpos;
if (chunk.size() > csize) {
System.arraycopy(chunk.getByteArray(), 0, buffer, bpos, csize);
bpos = buffer.length;
chunk.trim(csize, 0);
return buffer.length;
}
}
}
final int csize = chunk.size() < buffer.length - bpos ? chunk.size() : buffer.length - bpos;
if (csize > 0) {
System.arraycopy(chunk.getByteArray(), 0, buffer, bpos, csize);
bpos += csize;
chunk.trim(csize, 0);
return bpos;
} else {
return bpos > 0 ? bpos : -1;
}
} catch (XMLStreamException e) {
throw new IOException(e);
}
}
use of javax.xml.stream.XMLStreamException in project camel by apache.
the class XMLStreamReaderReader method ensureBuffering.
private int ensureBuffering(int size) throws IOException {
if (size < bpos) {
return bpos;
}
// refill the buffer as more buffering is requested than the current buffer status
try {
// very first event
if (XMLStreamConstants.START_DOCUMENT == reader.getEventType()) {
writer.writeStartDocument("utf-8", "1.0");
}
if (chunk.size() < buffer.length) {
while (reader.hasNext()) {
int code = reader.next();
switch(code) {
case XMLStreamConstants.END_DOCUMENT:
writer.writeEndDocument();
break;
case XMLStreamConstants.START_ELEMENT:
QName qname = reader.getName();
writer.writeStartElement(qname.getPrefix(), qname.getLocalPart(), qname.getNamespaceURI());
for (int i = 0; i < reader.getAttributeCount(); i++) {
writer.writeAttribute(reader.getAttributePrefix(i), reader.getAttributeNamespace(i), reader.getAttributeLocalName(i), reader.getAttributeValue(i));
}
for (int i = 0; i < reader.getNamespaceCount(); i++) {
writer.writeNamespace(reader.getNamespacePrefix(i), reader.getNamespaceURI(i));
}
break;
case XMLStreamConstants.END_ELEMENT:
writer.writeEndElement();
break;
case XMLStreamConstants.CHARACTERS:
writer.writeCharacters(reader.getText());
break;
case XMLStreamConstants.COMMENT:
writer.writeComment(reader.getText());
break;
case XMLStreamConstants.CDATA:
writer.writeCData(reader.getText());
break;
default:
break;
}
// check if the chunk is full
final int csize = buffer.length - bpos;
if (chunk.size() > csize) {
System.arraycopy(chunk.getCharArray(), 0, buffer, bpos, csize);
bpos = buffer.length;
chunk.trim(csize, 0);
return buffer.length;
}
}
}
final int csize = chunk.size() < buffer.length - bpos ? chunk.size() : buffer.length - bpos;
if (csize > 0) {
System.arraycopy(chunk.getCharArray(), 0, buffer, bpos, csize);
bpos += csize;
chunk.trim(csize, 0);
return bpos;
} else {
return bpos > 0 ? bpos : -1;
}
} catch (XMLStreamException e) {
throw new IOException(e);
}
}
use of javax.xml.stream.XMLStreamException in project hadoop by apache.
the class NodeInfo method checkConf.
public static List<String> checkConf(InputStream in) {
List<NodeInfo> nodes = null;
List<String> errors = new ArrayList<String>();
try {
nodes = parseConf(in);
if (nodes == null) {
errors.add("bad conf file: top-level element not <configuration>");
}
} catch (XMLStreamException e) {
errors.add("bad conf file: " + e.getMessage());
}
if (!errors.isEmpty()) {
return errors;
}
Map<String, List<Integer>> duplicatedProperties = new HashMap<String, List<Integer>>();
for (NodeInfo node : nodes) {
StartElement element = node.getStartElement();
int line = element.getLocation().getLineNumber();
if (!element.getName().equals(new QName("property"))) {
errors.add(String.format("Line %d: element not <property>", line));
continue;
}
List<XMLEvent> events = node.getXMLEventsForQName(new QName("name"));
if (events == null) {
errors.add(String.format("Line %d: <property> has no <name>", line));
} else {
String v = null;
for (XMLEvent event : events) {
if (event.isAttribute()) {
v = ((Attribute) event).getValue();
} else {
Characters c = node.getElement(event.asStartElement());
if (c != null) {
v = c.getData();
}
}
if (v == null || v.isEmpty()) {
errors.add(String.format("Line %d: <property> has an empty <name>", line));
}
}
if (v != null && !v.isEmpty()) {
List<Integer> lines = duplicatedProperties.get(v);
if (lines == null) {
lines = new ArrayList<Integer>();
duplicatedProperties.put(v, lines);
}
lines.add(node.getStartElement().getLocation().getLineNumber());
}
}
events = node.getXMLEventsForQName(new QName("value"));
if (events == null) {
errors.add(String.format("Line %d: <property> has no <value>", line));
}
for (QName qName : node.getDuplicatedQNames()) {
if (!qName.equals(new QName("source"))) {
errors.add(String.format("Line %d: <property> has duplicated <%s>s", line, qName));
}
}
}
for (Entry<String, List<Integer>> e : duplicatedProperties.entrySet()) {
List<Integer> lines = e.getValue();
if (1 < lines.size()) {
errors.add(String.format("Line %s: duplicated <property>s for %s", StringUtils.join(", ", lines), e.getKey()));
}
}
return errors;
}
use of javax.xml.stream.XMLStreamException in project hadoop by apache.
the class OfflineImageReconstructor method loadNodeChildrenHelper.
private void loadNodeChildrenHelper(Node parent, String expected, String[] terminators) throws IOException {
XMLEvent ev = null;
while (true) {
try {
ev = events.peek();
switch(ev.getEventType()) {
case XMLEvent.END_ELEMENT:
if (terminators.length != 0) {
return;
}
events.nextEvent();
return;
case XMLEvent.START_ELEMENT:
String key = ev.asStartElement().getName().getLocalPart();
for (String terminator : terminators) {
if (terminator.equals(key)) {
return;
}
}
events.nextEvent();
Node node = new Node();
parent.addChild(key, node);
loadNodeChildrenHelper(node, expected, new String[0]);
break;
case XMLEvent.CHARACTERS:
String val = XMLUtils.unmangleXmlString(ev.asCharacters().getData(), true);
parent.setVal(val);
events.nextEvent();
break;
case XMLEvent.ATTRIBUTE:
throw new IOException("Unexpected XML event " + ev);
default:
// Ignore other event types like comment, etc.
if (LOG.isTraceEnabled()) {
LOG.trace("Skipping XMLEvent " + ev);
}
events.nextEvent();
break;
}
} catch (XMLStreamException e) {
throw new IOException("Expecting " + expected + ", but got XMLStreamException", e);
}
}
}
use of javax.xml.stream.XMLStreamException in project hbase by apache.
the class RemoteAdmin method getInputStream.
/**
* Convert the REST server's response to an XML reader.
*
* @param response The REST server's response.
* @return A reader over the parsed XML document.
* @throws IOException If the document fails to parse
*/
private XMLStreamReader getInputStream(Response response) throws IOException {
try {
// Prevent the parser from reading XMl with external entities defined
XMLInputFactory xif = XMLInputFactory.newFactory();
xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
return xif.createXMLStreamReader(new ByteArrayInputStream(response.getBody()));
} catch (XMLStreamException e) {
throw new IOException("Failed to parse XML", e);
}
}
Aggregations