use of org.apache.xml.security.stax.ext.stax.XMLSecNamespace in project santuario-java by apache.
the class XMLSecurityUtils method getExcC14NInclusiveNamespacePrefixes.
public static Set<String> getExcC14NInclusiveNamespacePrefixes(XMLSecStartElement xmlSecStartElement, boolean excludeVisible) {
if (xmlSecStartElement == null) {
return Collections.emptySet();
}
Set<String> prefixes = new TreeSet<>();
XMLSecStartElement parentXMXmlSecStartElement = xmlSecStartElement.getParentXMLSecStartElement();
if (parentXMXmlSecStartElement != null) {
List<XMLSecNamespace> onElementDeclaredNamespaces = parentXMXmlSecStartElement.getOnElementDeclaredNamespaces();
List<XMLSecNamespace> xmlSecNamespaces = new ArrayList<>();
parentXMXmlSecStartElement.getNamespacesFromCurrentScope(xmlSecNamespaces);
xmlSecNamespaces = xmlSecNamespaces.subList(0, xmlSecNamespaces.size() - onElementDeclaredNamespaces.size());
// reverse iteration -> From current element namespaces to parent namespaces
for (int i = xmlSecNamespaces.size() - 1; i >= 0; i--) {
XMLSecNamespace xmlSecNamespace = xmlSecNamespaces.get(i);
String prefix = xmlSecNamespace.getPrefix();
if (prefix == null || prefix.isEmpty()) {
prefixes.add("#default");
} else {
prefixes.add(xmlSecNamespace.getPrefix());
}
}
if (excludeVisible) {
for (int i = 0; i < onElementDeclaredNamespaces.size(); i++) {
XMLSecNamespace xmlSecNamespace = onElementDeclaredNamespaces.get(i);
String prefix = xmlSecNamespace.getPrefix();
if (prefix == null || prefix.isEmpty()) {
prefixes.remove("#default");
} else {
prefixes.remove(prefix);
}
}
if (xmlSecStartElement.getName().getPrefix() == null || xmlSecStartElement.getName().getPrefix().isEmpty()) {
prefixes.remove("#default");
} else {
prefixes.remove(xmlSecStartElement.getName().getPrefix());
}
}
}
return prefixes;
}
use of org.apache.xml.security.stax.ext.stax.XMLSecNamespace in project santuario-java by apache.
the class XMLSecStartElementImpl method writeAsEncodedUnicode.
@Override
public void writeAsEncodedUnicode(Writer writer) throws XMLStreamException {
try {
writer.write('<');
final String prefix = getName().getPrefix();
if (prefix != null && !prefix.isEmpty()) {
writer.write(prefix);
writer.write(':');
}
writer.write(getName().getLocalPart());
for (Namespace xmlSecNamespace : namespaces) {
writer.write(" xmlns");
final String nsPrefix = xmlSecNamespace.getPrefix();
if (nsPrefix != null && !nsPrefix.isEmpty()) {
writer.write(':');
writer.write(nsPrefix);
}
writer.write("=\"");
writer.write(xmlSecNamespace.getValue());
writer.write('"');
}
for (Attribute xmlSecAttribute : attributes) {
writer.write(' ');
final String attrPrefix = xmlSecAttribute.getName().getPrefix();
if (attrPrefix != null && !attrPrefix.isEmpty()) {
writer.write(attrPrefix);
writer.write(':');
}
writer.write(xmlSecAttribute.getName().getLocalPart());
writer.write("=\"");
writer.write(xmlSecAttribute.getValue());
writer.write('"');
}
writer.write('>');
} catch (IOException e) {
throw new XMLStreamException(e);
}
}
use of org.apache.xml.security.stax.ext.stax.XMLSecNamespace in project santuario-java by apache.
the class XMLSecStartElementImpl method getNamespaceContext.
@Override
public NamespaceContext getNamespaceContext() {
return new NamespaceContext() {
@Override
public String getNamespaceURI(String prefix) {
for (int i = 0; i < namespaces.size(); i++) {
Namespace comparableNamespace = namespaces.get(i);
if (prefix.equals(comparableNamespace.getPrefix())) {
return comparableNamespace.getNamespaceURI();
}
}
if (parentXMLSecStartELement != null) {
return parentXMLSecStartELement.getNamespaceURI(prefix);
}
return null;
}
@Override
public String getPrefix(String namespaceURI) {
for (int i = 0; i < namespaces.size(); i++) {
Namespace comparableNamespace = namespaces.get(i);
if (namespaceURI.equals(comparableNamespace.getNamespaceURI())) {
return comparableNamespace.getPrefix();
}
}
if (parentXMLSecStartELement != null) {
return parentXMLSecStartELement.getNamespaceContext().getPrefix(namespaceURI);
}
return null;
}
@SuppressWarnings("rawtypes")
@Override
public Iterator getPrefixes(String namespaceURI) {
Set<String> prefixes = new HashSet<>();
List<XMLSecNamespace> xmlSecNamespaces = new ArrayList<>();
getNamespacesFromCurrentScope(xmlSecNamespaces);
for (int i = 0; i < xmlSecNamespaces.size(); i++) {
Namespace xmlSecNamespace = xmlSecNamespaces.get(i);
if (namespaceURI.equals(xmlSecNamespace.getNamespaceURI())) {
prefixes.add(xmlSecNamespace.getPrefix());
}
}
return prefixes.iterator();
}
};
}
Aggregations