use of javax.xml.stream.XMLEventReader in project uPortal by Jasig.
the class XsltPortalUrlProviderTest method setup.
@Before
public void setup() throws Exception {
final ResourceLoaderURIResolver resolver = new ResourceLoaderURIResolver(new ClassRelativeResourceLoader(getClass()));
this.tFactory = TransformerFactory.newInstance();
this.tFactory.setURIResolver(resolver);
this.tFactory.setErrorListener(new SimpleTransformErrorListener(LogFactory.getLog(getClass())));
// Load the XML document so it reads the same way the rendering pipeline reads XML
final XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
final XMLEventReader eventReader = xmlInputFactory.createXMLEventReader(this.getClass().getResourceAsStream("test.xml"));
final XMLStreamReader streamReader = new FixedXMLEventStreamReader(eventReader);
xmlSource = new StAXSource(streamReader);
xslTemplate = tFactory.newTemplates(new StreamSource(this.getClass().getResourceAsStream("test.xsl")));
expected = IOUtils.toString(this.getClass().getResourceAsStream("result.xml"));
}
use of javax.xml.stream.XMLEventReader in project Payara by payara.
the class DomainXmlTransformer method transform.
public File transform(boolean keepPorts) {
FileInputStream fis = null;
FileOutputStream fos = null;
XMLEventReader parser = null;
XMLEventWriter writer = null;
XMLInputFactory xif = (XMLInputFactory.class.getClassLoader() == null) ? XMLInputFactory.newInstance() : XMLInputFactory.newInstance(XMLInputFactory.class.getName(), XMLInputFactory.class.getClassLoader());
Set<String> empty_elements = (keepPorts) ? EMPTY_ELEMENTS_KEEP_PORTS : EMPTY_ELEMENTS;
try {
fis = new FileInputStream(in);
out = File.createTempFile("domain", "xml");
if (_logger.isLoggable(Level.FINE)) {
_logger.fine("[DomainXmlTransformer] Creating temp domain file: " + out);
}
if (System.getProperty(EJBContainerProviderImpl.KEEP_TEMPORARY_FILES) == null) {
out.deleteOnExit();
}
fos = new FileOutputStream(out);
parser = xif.createXMLEventReader(fis);
writer = xof.createXMLEventWriter(fos);
boolean fixedDasConfig = false;
while (parser.hasNext()) {
XMLEvent event = parser.nextEvent();
if (event.isStartElement()) {
String name = event.asStartElement().getName().getLocalPart();
if (SKIP_ELEMENTS.contains(name)) {
if (_logger.isLoggable(Level.FINE)) {
_logger.fine("[DomainXmlTransformer] Skipping all of: " + name);
}
getEndEventFor(parser, name);
continue;
}
boolean skip_to_end = false;
if (empty_elements.contains(name)) {
if (_logger.isLoggable(Level.FINE)) {
_logger.fine("[DomainXmlTransformer] Skipping details of: " + name);
}
skip_to_end = true;
} else if (SKIP_SETTINGS_ELEMENTS.contains(name)) {
// Make sure lazy init is not enabled by creating a new start element
// based on the original but that never includes the lazy init attribute
event = getSkippedElementStartEvent(event);
skip_to_end = true;
} else if (DISABLE_ELEMENTS.contains(name)) {
// Disable this element
event = getReplaceAttributeInStartEvent(event, ENABLED, FALSE);
skip_to_end = true;
} else if (JMS_HOST.equals(name)) {
// Set lazy-init to false
event = getReplaceAttributeInStartEvent(event, LAZY_INIT_ATTR, FALSE);
skip_to_end = true;
} else if (DAS_CONFIG.equals(name)) {
// Set dynamic-reload-enabled to false
event = getReplaceAttributeInStartEvent(event, DYNAMIC_RELOAD_ENABLED, FALSE);
fixedDasConfig = true;
skip_to_end = true;
} else if (JAVA_CONFIG.equals(name)) {
// Add jvm-options
writer.add(event);
event = getAddedEvent(event, writer, JVM_OPTIONS, INITIALIZE_ON_DEMAND);
}
if (skip_to_end) {
writer.add(event);
event = getEndEventFor(parser, name);
}
} else if (event.isEndElement()) {
String name = event.asEndElement().getName().getLocalPart();
if (ADMIN_SERVICE.equals(name)) {
if (!fixedDasConfig) {
writer.add(getAddedEventBeforeEndElement(event, writer, DAS_CONFIG, DYNAMIC_RELOAD_ENABLED, FALSE));
}
// for the next config
fixedDasConfig = false;
}
}
if (_logger.isLoggable(Level.FINEST)) {
_logger.finest("[DomainXmlTransformer] Processing: " + event);
}
writer.add(event);
}
writer.flush();
writer.close();
} catch (Exception e) {
_logger.log(Level.SEVERE, "ejb.embedded.tmp_file_create_error", e.getMessage());
_logger.log(Level.FINE, e.getMessage(), e);
return null;
} finally {
try {
if (parser != null) {
parser.close();
}
} catch (Exception e) {
}
try {
if (fis != null) {
fis.close();
}
} catch (Exception e) {
}
try {
if (writer != null) {
writer.close();
}
} catch (Exception e) {
}
try {
if (fos != null) {
fos.close();
}
} catch (Exception e) {
}
}
if (_logger.isLoggable(Level.FINE)) {
_logger.fine("[DomainXmlTransformer] Created temp domain file: " + out);
}
return out;
}
use of javax.xml.stream.XMLEventReader in project tomee by apache.
the class StaxCompare method compare.
public static void compare(final String a, final String b) throws Exception {
final StringBuilder message = new StringBuilder();
final XMLInputFactory factory = XMLInputFactory.newInstance();
factory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
final XMLEventReader rA = factory.createXMLEventReader(new StringReader(a));
final XMLEventReader rB = factory.createXMLEventReader(new StringReader(b));
if (!compare(rA, rB, message)) {
throw new Exception(message.toString());
}
}
use of javax.xml.stream.XMLEventReader in project languagetool by languagetool-org.
the class IpaExtractor method run.
private void run(FileInputStream fis) throws XMLStreamException {
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLEventReader reader = factory.createXMLEventReader(fis);
String title = null;
while (reader.hasNext()) {
XMLEvent event = reader.nextEvent();
if (event.getEventType() == XMLStreamConstants.START_ELEMENT) {
String elementName = event.asStartElement().getName().getLocalPart();
switch(elementName) {
case "title":
XMLEvent nextEvent = reader.nextEvent();
title = nextEvent.asCharacters().getData();
articleCount++;
break;
case "text":
ipaCount += handleTextElement(title, reader);
break;
}
}
}
}
use of javax.xml.stream.XMLEventReader in project languagetool by languagetool-org.
the class AtomFeedParser method getAtomFeedItems.
List<AtomFeedItem> getAtomFeedItems(InputStream xml) throws XMLStreamException {
String id = null;
String title = null;
Date date = null;
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLEventReader eventReader = inputFactory.createXMLEventReader(xml);
try {
List<AtomFeedItem> items = new ArrayList<>();
while (eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();
if (event.isStartElement()) {
String localPart = event.asStartElement().getName().getLocalPart();
switch(localPart) {
case "id":
id = getCharacterData(eventReader);
break;
case "title":
title = getCharacterData(eventReader);
break;
case "updated":
String dateString = getCharacterData(eventReader);
try {
// e.g. 2013-12-03T09:48:29Z - got this from http://stackoverflow.com/questions/6038136,
// with SimpleDateParser the hour is off by one:
date = DatatypeFactory.newInstance().newXMLGregorianCalendar(dateString).toGregorianCalendar().getTime();
} catch (Exception e) {
throw new RuntimeException("Could not parse date string '" + dateString + "'", e);
}
break;
case "summary":
if (id == null || title == null || date == null) {
throw new RuntimeException("id, title and/or date is null: id=" + id + ", title=" + title + ", date=" + date);
}
items.add(new AtomFeedItem(id, title, getCharacterData(eventReader), date));
id = null;
title = null;
date = null;
break;
}
}
}
return items;
} finally {
eventReader.close();
}
}
Aggregations