use of javax.xml.stream.events.EndElement in project midpoint by Evolveum.
the class MidpointFunctionsImpl method parseXmlToMap.
@Override
public Map<String, String> parseXmlToMap(String xml) {
Map<String, String> resultingMap = new HashMap<>();
if (xml != null && !xml.isEmpty()) {
XMLInputFactory factory = XMLInputFactory.newInstance();
String value = "";
String startName = "";
InputStream stream = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));
boolean isRootElement = true;
try {
XMLEventReader eventReader = factory.createXMLEventReader(stream);
while (eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();
int code = event.getEventType();
if (code == XMLStreamConstants.START_ELEMENT) {
StartElement startElement = event.asStartElement();
startName = startElement.getName().getLocalPart();
if (!isRootElement) {
resultingMap.put(startName, null);
} else {
isRootElement = false;
}
} else if (code == XMLStreamConstants.CHARACTERS) {
Characters characters = event.asCharacters();
if (!characters.isWhiteSpace()) {
StringBuilder valueBuilder;
if (value != null) {
valueBuilder = new StringBuilder(value).append(" ").append(characters.getData());
} else {
valueBuilder = new StringBuilder(characters.getData());
}
value = valueBuilder.toString();
}
} else if (code == XMLStreamConstants.END_ELEMENT) {
EndElement endElement = event.asEndElement();
String endName = endElement.getName().getLocalPart();
if (endName.equals(startName)) {
if (value != null) {
resultingMap.put(endName, value);
value = null;
}
} else {
LOGGER.trace("No value between xml tags, tag name : {}", endName);
}
} else if (code == XMLStreamConstants.END_DOCUMENT) {
isRootElement = true;
}
}
} catch (XMLStreamException e) {
StringBuilder error = new StringBuilder("Xml stream exception wile parsing xml string").append(e.getLocalizedMessage());
throw new SystemException(error.toString());
}
} else {
LOGGER.trace("Input xml string null or empty.");
}
return resultingMap;
}
use of javax.xml.stream.events.EndElement in project bazel by bazelbuild.
the class XmlResourceValues method readContentsAsString.
/**
* Reads the xml events as a string until finding a closing tag.
*
* @param eventReader The current xml stream.
* @param startTag The name of the tag to close on.
* @param namespacesCollector A builder for collecting namespaces.
* @return A xml escaped string representation of the xml stream
*/
@Nullable
public static String readContentsAsString(XMLEventReader eventReader, QName startTag, Namespaces.Collector namespacesCollector) throws XMLStreamException {
StringWriter contents = new StringWriter();
XMLEventWriter writer = XML_OUTPUT_FACTORY.createXMLEventWriter(contents);
while (!isEndTag(eventReader.peek(), startTag)) {
XMLEvent xmlEvent = (XMLEvent) eventReader.next();
if (xmlEvent.isStartElement()) {
namespacesCollector.collectFrom(xmlEvent.asStartElement());
writer.add(xmlEvent);
} else {
writer.add(xmlEvent);
}
}
// Verify the end element.
EndElement endElement = eventReader.nextEvent().asEndElement();
Preconditions.checkArgument(endElement.getName().equals(startTag));
return contents.toString();
}
use of javax.xml.stream.events.EndElement in project uPortal by Jasig.
the class TransientUserLayoutXMLEventReader method getAdditionalEvents.
/**
* Examines the current token and when appropriate creates and returns dynamically created
* content. If dynamic content is not created, return null.
*
* @param event The current event
* @return Dynamic content to inject into document, else null if no additional dynamic content
* was created.
*/
@Override
protected Deque<XMLEvent> getAdditionalEvents(XMLEvent event) {
if (event.isStartElement()) {
final StartElement startElement = event.asStartElement();
// All following logic requires an ID attribute, ignore any element without one
final Attribute idAttribute = startElement.getAttributeByName(IUserLayoutManager.ID_ATTR_NAME);
if (idAttribute == null) {
return null;
}
// Create and return a transient (dynamically created) folder that includes a transient
// channel
// if we are processing the tart element of the root node
// iff the subscribeId is present and describes a transient channel and not a regular
// layout channel.
final String subscribeId = this.userLayoutManager.getFocusedId();
if (this.rootFolderId.equals(idAttribute.getValue()) && subscribeId != null && !subscribeId.equals("") && this.userLayoutManager.isTransientChannel(subscribeId)) {
IPortletDefinition chanDef = null;
try {
chanDef = this.userLayoutManager.getChannelDefinition(subscribeId);
} catch (Exception e) {
logger.error("Could not obtain IChannelDefinition for subscribe id: {}", subscribeId, e);
}
if (chanDef != null) {
final QName name = startElement.getName();
final String namespaceURI = name.getNamespaceURI();
final String prefix = name.getPrefix();
final Deque<XMLEvent> transientEventBuffer = new LinkedList<XMLEvent>();
final Collection<Attribute> transientFolderAttributes = new LinkedList<Attribute>();
transientFolderAttributes.add(EVENT_FACTORY.createAttribute("ID", TransientUserLayoutManagerWrapper.TRANSIENT_FOLDER_ID));
transientFolderAttributes.add(EVENT_FACTORY.createAttribute("name", chanDef != null ? chanDef.getTitle() : "Temporary"));
transientFolderAttributes.add(EVENT_FACTORY.createAttribute("type", "regular"));
transientFolderAttributes.add(EVENT_FACTORY.createAttribute("hidden", "false"));
transientFolderAttributes.add(EVENT_FACTORY.createAttribute("unremovable", "true"));
transientFolderAttributes.add(EVENT_FACTORY.createAttribute("immutable", "true"));
transientFolderAttributes.add(EVENT_FACTORY.createAttribute("unremovable", "true"));
transientFolderAttributes.add(EVENT_FACTORY.createAttribute("dlm:addChildAllowed", "false"));
transientFolderAttributes.add(EVENT_FACTORY.createAttribute("dlm:deleteAllowed", "false"));
transientFolderAttributes.add(EVENT_FACTORY.createAttribute("dlm:editAllowed", "false"));
transientFolderAttributes.add(EVENT_FACTORY.createAttribute("dlm:moveAllowed", "false"));
transientFolderAttributes.add(EVENT_FACTORY.createAttribute("dlm:precedence", "100.0"));
transientFolderAttributes.add(EVENT_FACTORY.createAttribute("transient", "true"));
final StartElement transientFolder = EVENT_FACTORY.createStartElement(prefix, namespaceURI, IUserLayoutManager.FOLDER, transientFolderAttributes.iterator(), null);
transientEventBuffer.add(transientFolder);
// TODO Move IChannelDefinition/IPortletDefinition -> StAX events code somewhere
// reusable
final Collection<Attribute> channelAttrs = new LinkedList<Attribute>();
channelAttrs.add(EVENT_FACTORY.createAttribute("ID", subscribeId));
channelAttrs.add(EVENT_FACTORY.createAttribute("typeID", Integer.toString(chanDef.getType().getId())));
channelAttrs.add(EVENT_FACTORY.createAttribute("hidden", "false"));
channelAttrs.add(EVENT_FACTORY.createAttribute("unremovable", "true"));
channelAttrs.add(EVENT_FACTORY.createAttribute("dlm:deleteAllowed", "false"));
channelAttrs.add(EVENT_FACTORY.createAttribute("dlm:moveAllowed", "false"));
channelAttrs.add(EVENT_FACTORY.createAttribute("name", chanDef.getName()));
channelAttrs.add(EVENT_FACTORY.createAttribute("description", chanDef.getDescription()));
channelAttrs.add(EVENT_FACTORY.createAttribute("title", chanDef.getTitle()));
channelAttrs.add(EVENT_FACTORY.createAttribute("chanID", chanDef.getPortletDefinitionId().getStringId()));
channelAttrs.add(EVENT_FACTORY.createAttribute("fname", chanDef.getFName()));
channelAttrs.add(EVENT_FACTORY.createAttribute("timeout", Integer.toString(chanDef.getTimeout())));
channelAttrs.add(EVENT_FACTORY.createAttribute("transient", "true"));
final StartElement startChannel = EVENT_FACTORY.createStartElement(prefix, namespaceURI, IUserLayoutManager.CHANNEL, channelAttrs.iterator(), null);
transientEventBuffer.offer(startChannel);
// add channel parameter elements
for (final IPortletDefinitionParameter parm : chanDef.getParameters()) {
final Collection<Attribute> parameterAttrs = new LinkedList<Attribute>();
parameterAttrs.add(EVENT_FACTORY.createAttribute("name", parm.getName()));
parameterAttrs.add(EVENT_FACTORY.createAttribute("value", parm.getValue()));
final StartElement startParameter = EVENT_FACTORY.createStartElement(prefix, namespaceURI, IUserLayoutManager.PARAMETER, parameterAttrs.iterator(), null);
transientEventBuffer.offer(startParameter);
final EndElement endParameter = EVENT_FACTORY.createEndElement(prefix, namespaceURI, IUserLayoutManager.PARAMETER, null);
transientEventBuffer.offer(endParameter);
}
final EndElement endChannel = EVENT_FACTORY.createEndElement(prefix, namespaceURI, IUserLayoutManager.CHANNEL, null);
transientEventBuffer.offer(endChannel);
final EndElement endFolder = EVENT_FACTORY.createEndElement(prefix, namespaceURI, IUserLayoutManager.FOLDER, null);
transientEventBuffer.offer(endFolder);
return transientEventBuffer;
} else {
// I don't think subscribeId could be null, but log warning if so.
logger.warn("Unable to resolve portlet definition for subscribe ID {}", subscribeId);
}
}
}
return null;
}
use of javax.xml.stream.events.EndElement in project uPortal by Jasig.
the class FilteringXMLEventReader method internalNext.
protected final XMLEvent internalNext(boolean peek) throws XMLStreamException {
XMLEvent event = null;
if (peekedEvent != null) {
event = peekedEvent;
peekedEvent = null;
return event;
}
do {
event = super.getParent().nextEvent();
// If there are pruned elements in the queue filtering events is still needed
if (!prunedElements.isEmpty()) {
// If another start element add it to the queue
if (event.isStartElement()) {
final StartElement startElement = event.asStartElement();
prunedElements.push(startElement.getName());
} else // start/end elements match up
if (event.isEndElement()) {
final QName startElementName = prunedElements.pop();
final EndElement endElement = event.asEndElement();
final QName endElementName = endElement.getName();
if (!startElementName.equals(endElementName)) {
throw new IllegalArgumentException("Malformed XMLEvent stream. Expected end element for " + startElementName + " but found end element for " + endElementName);
}
}
event = null;
} else {
final XMLEvent filteredEvent = this.filterEvent(event, peek);
// end element need to be removed as well
if (filteredEvent == null && event.isStartElement()) {
final StartElement startElement = event.asStartElement();
final QName name = startElement.getName();
prunedElements.push(name);
}
event = filteredEvent;
}
} while (event == null);
return event;
}
use of javax.xml.stream.events.EndElement in project cxf by apache.
the class StaxEventFilter method accept.
public boolean accept(XMLEvent event) {
if (event.isStartDocument() || event.isEndDocument()) {
return false;
}
if (event.isStartElement()) {
StartElement startEl = event.asStartElement();
QName elName = startEl.getName();
for (QName tag : tags) {
if (elName.equals(tag)) {
return false;
}
}
}
if (event.isEndElement()) {
EndElement endEl = event.asEndElement();
QName elName = endEl.getName();
for (QName tag : tags) {
if (elName.equals(tag)) {
return false;
}
}
}
return true;
}
Aggregations