use of javax.xml.stream.events.XMLEvent in project teiid by teiid.
the class XMLSystemFunctions method serialize.
public static Object serialize(XMLSerialize xs, XMLType value) throws TransformationException {
Type type = value.getType();
final Charset encoding;
if (xs.getEncoding() != null) {
encoding = Charset.forName(xs.getEncoding());
} else {
encoding = UTF_8;
}
if (Boolean.TRUE.equals(xs.getDeclaration())) {
// need to replace existing/default declaration
if (type == Type.ELEMENT || type == Type.DOCUMENT) {
XMLEventFactory xmlEventFactory = threadLocalEventtFactory.get();
xmlEventFactory.setLocation(dummyLocation);
XMLEvent start = null;
if (xs.getVersion() != null) {
start = xmlEventFactory.createStartDocument(encoding.name(), xs.getVersion());
} else {
// use the encoding regardless as different stax impls have different default
// behavior
start = xmlEventFactory.createStartDocument(encoding.name());
}
StAXSourceProvider sourceProvider = new DeclarationStaxSourceProvider(start, value);
value = new XMLType(new StAXSQLXML(sourceProvider, encoding));
value.setType(type);
}
// else just ignore, since the result is likely invalid
} else if (type == Type.DOCUMENT && Boolean.FALSE.equals(xs.getDeclaration())) {
final XMLType v = value;
StAXSourceProvider sourceProvider = new StAXSourceProvider() {
@Override
public StAXSource getStaxSource() throws SQLException {
try {
XMLEventReader eventReader = getXMLEventReader(v.getSource(StAXSource.class));
eventReader = XMLType.getXmlInputFactory().createFilteredReader(eventReader, declarationOmittingFilter);
return new StAXSource(eventReader);
} catch (XMLStreamException e) {
throw new SQLException(e);
}
}
};
value = new XMLType(new StAXSQLXML(sourceProvider, encoding));
value.setType(Type.DOCUMENT);
}
if (xs.getType() == DataTypeManager.DefaultDataClasses.STRING) {
return DataTypeManager.transformValue(value, xs.getType());
}
if (xs.getType() == DataTypeManager.DefaultDataClasses.CLOB) {
InputStreamFactory isf = Evaluator.getInputStreamFactory(value);
return new ClobType(new ClobImpl(isf, -1));
}
if (xs.getType() == DataTypeManager.DefaultDataClasses.VARBINARY) {
try {
InputStream is = null;
if (!Charset.forName(value.getEncoding()).equals(encoding)) {
is = new ReaderInputStream(value.getCharacterStream(), encoding);
} else {
is = value.getBinaryStream();
}
byte[] bytes = ObjectConverterUtil.convertToByteArray(is, DataTypeManager.MAX_VARBINARY_BYTES);
return new BinaryType(bytes);
} catch (SQLException e) {
// $NON-NLS-1$ //$NON-NLS-2$
throw new TransformationException(CorePlugin.Event.TEIID10080, e, CorePlugin.Util.gs(CorePlugin.Event.TEIID10080, "XML", "VARBINARY"));
} catch (IOException e) {
// $NON-NLS-1$ //$NON-NLS-2$
throw new TransformationException(CorePlugin.Event.TEIID10080, e, CorePlugin.Util.gs(CorePlugin.Event.TEIID10080, "XML", "VARBINARY"));
}
}
InputStreamFactory isf = null;
if (!Charset.forName(value.getEncoding()).equals(encoding)) {
// create a wrapper for the input stream
isf = new InputStreamFactory.SQLXMLInputStreamFactory(value) {
public InputStream getInputStream() throws IOException {
try {
return new ReaderInputStream(sqlxml.getCharacterStream(), encoding);
} catch (SQLException e) {
throw new IOException(e);
}
}
};
} else {
isf = Evaluator.getInputStreamFactory(value);
}
return new BlobType(new BlobImpl(isf));
}
use of javax.xml.stream.events.XMLEvent in project teiid by teiid.
the class TestXMLSystemFunctions method testRepairingNamespaces.
@Test
public void testRepairingNamespaces() throws Exception {
XMLOutputFactory factory = XMLSystemFunctions.getOutputFactory(true);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLEventWriter writer = factory.createXMLEventWriter(baos);
XMLEventReader reader = XMLType.getXmlInputFactory().createXMLEventReader(new StringReader("<a xmlns:x=\"http://foo\"><b x:y=\"1\"/></a>"));
reader.nextTag();
reader = XMLType.getXmlInputFactory().createFilteredReader(reader, new EventFilter() {
@Override
public boolean accept(XMLEvent arg0) {
if (arg0.isStartDocument() || arg0.isEndDocument()) {
return false;
}
if (arg0.isEndElement() && ((EndElement) arg0).getName().getLocalPart().equals("a")) {
return false;
}
return true;
}
});
writer.add(reader);
writer.close();
assertEquals("<b xmlns=\"\" xmlns:x=\"http://foo\" x:y=\"1\"></b>", new String(baos.toByteArray(), "UTF-8"));
}
use of javax.xml.stream.events.XMLEvent in project bw-calendar-engine by Bedework.
the class DmozStructure method processInFile.
boolean processInFile(File f) throws Throwable {
XMLInputFactory factory = XMLInputFactory.newInstance();
Reader fileReader = new FileReader(f);
XMLEventReader reader = factory.createXMLEventReader(fileReader);
while (reader.hasNext()) {
XMLEvent event = reader.nextEvent();
if (skippingElement != null) {
if (event.isEndElement()) {
QName elname = event.asEndElement().getName();
if (elname.equals(skippingElement)) {
skippingElement = null;
}
}
continue;
}
if (event.isStartElement()) {
processStartElement(reader, event.asStartElement());
continue;
}
if (event.isEndElement()) {
processEndElement(reader, event.asEndElement());
continue;
}
if (event.isCharacters()) {
String s = event.asCharacters().getData();
if (curText != null) {
curText.append(s);
continue;
}
if (s.trim().length() == 0) {
continue;
}
info("Text: " + s);
continue;
}
}
return true;
}
use of javax.xml.stream.events.XMLEvent in project tutorials by eugenp.
the class StaxParser method getAllTutorial.
public List<Tutorial> getAllTutorial() {
boolean bTitle = false;
boolean bDescription = false;
boolean bDate = false;
boolean bAuthor = false;
List<Tutorial> tutorials = new ArrayList<Tutorial>();
try {
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLEventReader eventReader = factory.createXMLEventReader(new FileReader(this.getFile()));
Tutorial current = null;
while (eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();
switch(event.getEventType()) {
case XMLStreamConstants.START_ELEMENT:
StartElement startElement = event.asStartElement();
String qName = startElement.getName().getLocalPart();
if (qName.equalsIgnoreCase("tutorial")) {
current = new Tutorial();
Iterator<Attribute> attributes = startElement.getAttributes();
while (attributes.hasNext()) {
Attribute currentAt = attributes.next();
if (currentAt.getName().toString().equalsIgnoreCase("tutId")) {
current.setTutId(currentAt.getValue());
} else if (currentAt.getName().toString().equalsIgnoreCase("type")) {
current.setType(currentAt.getValue());
}
}
} else if (qName.equalsIgnoreCase("title")) {
bTitle = true;
} else if (qName.equalsIgnoreCase("description")) {
bDescription = true;
} else if (qName.equalsIgnoreCase("date")) {
bDate = true;
} else if (qName.equalsIgnoreCase("author")) {
bAuthor = true;
}
break;
case XMLStreamConstants.CHARACTERS:
Characters characters = event.asCharacters();
if (bTitle) {
if (current != null) {
current.setTitle(characters.getData());
}
bTitle = false;
}
if (bDescription) {
if (current != null) {
current.setDescription(characters.getData());
}
bDescription = false;
}
if (bDate) {
if (current != null) {
current.setDate(characters.getData());
}
bDate = false;
}
if (bAuthor) {
if (current != null) {
current.setAuthor(characters.getData());
}
bAuthor = false;
}
break;
case XMLStreamConstants.END_ELEMENT:
EndElement endElement = event.asEndElement();
if (endElement.getName().getLocalPart().equalsIgnoreCase("tutorial")) {
if (current != null) {
tutorials.add(current);
}
}
break;
}
}
} catch (FileNotFoundException | XMLStreamException e) {
e.printStackTrace();
}
return tutorials;
}
use of javax.xml.stream.events.XMLEvent in project teiid by teiid.
the class XMLReader method read.
@Override
public int read() throws IOException {
while (pos >= builder.length()) {
if (!reader.hasNext()) {
return -1;
}
if (builder.length() > BUFFER_SIZE) {
builder.setLength(0);
pos = 0;
}
try {
XMLEvent event = reader.nextEvent();
writer.add(event);
writer.flush();
} catch (XMLStreamException e) {
throw new IOException(e);
}
}
return builder.charAt(pos++);
}
Aggregations