use of javax.xml.stream.events.Attribute in project hibernate-orm by hibernate.
the class JpaNamespaceTransformingEventReader method updateElementAttributes.
private List<Attribute> updateElementAttributes(StartElement startElement) {
// adjust the version attribute
List<Attribute> newElementAttributeList = new ArrayList<Attribute>();
Iterator<?> existingAttributesIterator = startElement.getAttributes();
while (existingAttributesIterator.hasNext()) {
Attribute attribute = (Attribute) existingAttributesIterator.next();
if (VERSION_ATTRIBUTE_NAME.equals(attribute.getName().getLocalPart())) {
if (!DEFAULT_VERSION.equals(attribute.getName().getPrefix())) {
newElementAttributeList.add(xmlEventFactory.createAttribute(attribute.getName(), DEFAULT_VERSION));
}
} else {
newElementAttributeList.add(attribute);
}
}
return newElementAttributeList;
}
use of javax.xml.stream.events.Attribute in project languagetool by languagetool-org.
the class XmlUsageCounter method countElementsAndAttributes.
private void countElementsAndAttributes(InputStream in) throws XMLStreamException {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
while (eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();
if (event.isStartElement()) {
String elementName = event.asStartElement().getName().getLocalPart();
add(elementName);
Iterator attributes = event.asStartElement().getAttributes();
while (attributes.hasNext()) {
Attribute att = (Attribute) attributes.next();
add(elementName + "/" + att.getName());
}
}
}
}
use of javax.xml.stream.events.Attribute in project bazel by bazelbuild.
the class XmlResourceValues method parseTagAttributes.
public static Map<String, String> parseTagAttributes(StartElement start) {
// Using a map to deduplicate xmlns declarations on the attributes.
Map<String, String> attributeMap = new LinkedHashMap<>();
Iterator<Attribute> attributes = iterateAttributesFrom(start);
while (attributes.hasNext()) {
Attribute attribute = attributes.next();
QName name = attribute.getName();
// Name used as the resource key, so skip it here.
if (ATTR_NAME.equals(name)) {
continue;
}
String value = escapeXmlValues(attribute.getValue()).replace("\"", """);
if (!name.getNamespaceURI().isEmpty()) {
attributeMap.put(name.getPrefix() + ":" + attribute.getName().getLocalPart(), value);
} else {
attributeMap.put(attribute.getName().getLocalPart(), value);
}
Iterator<Namespace> namespaces = iterateNamespacesFrom(start);
while (namespaces.hasNext()) {
Namespace namespace = namespaces.next();
attributeMap.put("xmlns:" + namespace.getPrefix(), namespace.getNamespaceURI());
}
}
return attributeMap;
}
use of javax.xml.stream.events.Attribute in project bazel by bazelbuild.
the class DataValueFileWithIds method parse.
public static void parse(XMLInputFactory xmlInputFactory, Path source, FullyQualifiedName fileKey, FullyQualifiedName.Factory fqnFactory, KeyValueConsumer<DataKey, DataResource> overwritingConsumer, KeyValueConsumer<DataKey, DataResource> combiningConsumer) throws IOException, XMLStreamException {
ImmutableSet.Builder<String> newIds = ImmutableSet.builder();
try (BufferedInputStream inStream = new BufferedInputStream(Files.newInputStream(source))) {
XMLEventReader eventReader = xmlInputFactory.createXMLEventReader(inStream, StandardCharsets.UTF_8.toString());
// forgiving and allow even non-android namespaced attributes to define a new ID.
while (eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();
if (event.isStartElement()) {
StartElement start = event.asStartElement();
Iterator<Attribute> attributes = XmlResourceValues.iterateAttributesFrom(start);
while (attributes.hasNext()) {
Attribute attribute = attributes.next();
String value = attribute.getValue();
if (value.startsWith(SdkConstants.NEW_ID_PREFIX)) {
String idName = value.substring(SdkConstants.NEW_ID_PREFIX.length());
newIds.add(idName);
}
}
}
}
eventReader.close();
} catch (XMLStreamException e) {
throw new XMLStreamException(source + ": " + e.getMessage(), e.getLocation(), e);
} catch (RuntimeException e) {
throw new RuntimeException("Error parsing " + source, e);
}
ImmutableSet<String> idResources = newIds.build();
overwritingConsumer.consume(fileKey, DataValueFile.of(source));
for (String id : idResources) {
combiningConsumer.consume(fqnFactory.create(ResourceType.ID, id), DataResourceXml.createWithNoNamespace(source, IdXmlResourceValue.of()));
}
}
use of javax.xml.stream.events.Attribute in project bazel by bazelbuild.
the class AndroidManifestProcessor method writeManifestPackage.
/**
* Overwrite the package attribute of {@code <manifest>} in an AndroidManifest.xml file.
*
* @param manifest The input manifest.
* @param customPackage The package to write to the manifest.
* @param output The output manifest to generate.
* @return The output manifest if generated or the input manifest if no overwriting is required.
*/
/* TODO(apell): switch from custom xml parsing to Gradle merger with NO_PLACEHOLDER_REPLACEMENT
* set when android common is updated to version 2.5.0.
*/
public Path writeManifestPackage(Path manifest, String customPackage, Path output) {
if (Strings.isNullOrEmpty(customPackage)) {
return manifest;
}
try {
Files.createDirectories(output.getParent());
XMLEventReader reader = XMLInputFactory.newInstance().createXMLEventReader(Files.newInputStream(manifest), UTF_8.name());
XMLEventWriter writer = XMLOutputFactory.newInstance().createXMLEventWriter(Files.newOutputStream(output), UTF_8.name());
XMLEventFactory eventFactory = XMLEventFactory.newInstance();
while (reader.hasNext()) {
XMLEvent event = reader.nextEvent();
if (event.isStartElement() && event.asStartElement().getName().toString().equalsIgnoreCase("manifest")) {
StartElement element = event.asStartElement();
@SuppressWarnings("unchecked") Iterator<Attribute> attributes = element.getAttributes();
ImmutableList.Builder<Attribute> newAttributes = ImmutableList.builder();
while (attributes.hasNext()) {
Attribute attr = attributes.next();
if (attr.getName().toString().equalsIgnoreCase("package")) {
newAttributes.add(eventFactory.createAttribute("package", customPackage));
} else {
newAttributes.add(attr);
}
}
writer.add(eventFactory.createStartElement(element.getName(), newAttributes.build().iterator(), element.getNamespaces()));
} else {
writer.add(event);
}
}
writer.flush();
} catch (XMLStreamException | FactoryConfigurationError | IOException e) {
throw new RuntimeException(e);
}
return output;
}
Aggregations