use of org.apache.xml.security.stax.ext.stax.XMLSecNamespace in project santuario-java by apache.
the class AbstractDecryptInputProcessor method writeWrapperStartElement.
private InputStream writeWrapperStartElement(XMLSecStartElement xmlSecStartElement) throws IOException {
// temporary writer to write the dummy wrapper element with all namespaces in the current scope
// spec says (4.2): "The cleartext octet sequence obtained in step 3 is interpreted as UTF-8 encoded character data."
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append('<');
stringBuilder.append(wrapperElementName.getPrefix());
stringBuilder.append(':');
stringBuilder.append(wrapperElementName.getLocalPart());
stringBuilder.append(' ');
stringBuilder.append("xmlns:");
stringBuilder.append(wrapperElementName.getPrefix());
stringBuilder.append("=\"");
stringBuilder.append(wrapperElementName.getNamespaceURI());
stringBuilder.append('\"');
// apply all namespaces from current scope to get a valid documentfragment:
List<XMLSecNamespace> comparableNamespacesToApply = new ArrayList<>();
List<XMLSecNamespace> comparableNamespaceList = new ArrayList<>();
xmlSecStartElement.getNamespacesFromCurrentScope(comparableNamespaceList);
// reverse iteration -> From current element namespaces to parent namespaces
for (int i = comparableNamespaceList.size() - 1; i >= 0; i--) {
XMLSecNamespace comparableNamespace = comparableNamespaceList.get(i);
if (!comparableNamespacesToApply.contains(comparableNamespace)) {
comparableNamespacesToApply.add(comparableNamespace);
stringBuilder.append(' ');
String prefix = comparableNamespace.getPrefix();
String uri = comparableNamespace.getNamespaceURI();
if (prefix == null || prefix.isEmpty()) {
stringBuilder.append("xmlns=\"");
stringBuilder.append(uri);
stringBuilder.append("\"");
} else {
stringBuilder.append("xmlns:");
stringBuilder.append(prefix);
stringBuilder.append("=\"");
stringBuilder.append(uri);
stringBuilder.append("\"");
}
}
}
stringBuilder.append('>');
return new UnsyncByteArrayInputStream(stringBuilder.toString().getBytes(StandardCharsets.UTF_8));
}
use of org.apache.xml.security.stax.ext.stax.XMLSecNamespace in project santuario-java by apache.
the class XMLSecNamespaceImpl method getInstance.
public static XMLSecNamespace getInstance(String prefix, String uri) {
String prefixToUse = prefix;
if (prefixToUse == null) {
prefixToUse = "";
}
// sun's stax parser returns null for the default namespace
String uriToUse = uri;
if (uriToUse == null) {
uriToUse = "";
}
Map<String, XMLSecNamespace> nsMap = xmlSecNamespaceMap.get(prefixToUse);
if (nsMap != null) {
XMLSecNamespace xmlSecNamespace = nsMap.get(uriToUse);
if (xmlSecNamespace != null) {
return xmlSecNamespace;
} else {
xmlSecNamespace = new XMLSecNamespaceImpl(prefixToUse, uriToUse);
nsMap.put(uriToUse, xmlSecNamespace);
return xmlSecNamespace;
}
} else {
nsMap = new WeakHashMap<String, XMLSecNamespace>();
XMLSecNamespace xmlSecNamespace = new XMLSecNamespaceImpl(prefixToUse, uriToUse);
nsMap.put(uriToUse, xmlSecNamespace);
xmlSecNamespaceMap.put(prefixToUse, nsMap);
return xmlSecNamespace;
}
}
use of org.apache.xml.security.stax.ext.stax.XMLSecNamespace in project santuario-java by apache.
the class AbstractOutputProcessor method createStartElementAndOutputAsEvent.
public XMLSecStartElement createStartElementAndOutputAsEvent(OutputProcessorChain outputProcessorChain, QName element, boolean outputLocalNs, List<XMLSecAttribute> attributes) throws XMLStreamException, XMLSecurityException {
List<XMLSecNamespace> comparableNamespaces = Collections.emptyList();
if (outputLocalNs) {
comparableNamespaces = new ArrayList<>(2);
comparableNamespaces.add(XMLSecEventFactory.createXMLSecNamespace(element.getPrefix(), element.getNamespaceURI()));
}
if (attributes != null) {
for (int i = 0; i < attributes.size(); i++) {
XMLSecAttribute xmlSecAttribute = attributes.get(i);
QName attributeName = xmlSecAttribute.getName();
String attributeNamePrefix = attributeName.getPrefix();
if (attributeNamePrefix != null && attributeNamePrefix.isEmpty()) {
continue;
}
if (!comparableNamespaces.contains(xmlSecAttribute.getAttributeNamespace())) {
if (comparableNamespaces == Collections.<XMLSecNamespace>emptyList()) {
comparableNamespaces = new ArrayList<>(1);
}
comparableNamespaces.add(xmlSecAttribute.getAttributeNamespace());
}
}
}
XMLSecStartElement xmlSecStartElement = XMLSecEventFactory.createXmlSecStartElement(element, attributes, comparableNamespaces);
outputAsEvent(outputProcessorChain, xmlSecStartElement);
return xmlSecStartElement;
}
use of org.apache.xml.security.stax.ext.stax.XMLSecNamespace in project santuario-java by apache.
the class AbstractOutputProcessor method addAttributes.
public XMLSecStartElement addAttributes(XMLSecStartElement xmlSecStartElement, List<XMLSecAttribute> attributeList) throws XMLStreamException {
List<XMLSecNamespace> declaredNamespaces = xmlSecStartElement.getOnElementDeclaredNamespaces();
for (int i = 0; i < attributeList.size(); i++) {
XMLSecAttribute xmlSecAttribute = attributeList.get(i);
xmlSecStartElement.addAttribute(xmlSecAttribute);
final QName attributeName = xmlSecAttribute.getName();
if (attributeName.getNamespaceURI() != null && !"".equals(attributeName.getNamespaceURI()) && !declaredNamespaces.contains(xmlSecAttribute.getAttributeNamespace())) {
xmlSecStartElement.addNamespace(xmlSecAttribute.getAttributeNamespace());
}
}
return xmlSecStartElement;
}
use of org.apache.xml.security.stax.ext.stax.XMLSecNamespace in project santuario-java by apache.
the class AbstractOutputProcessor method outputDOMElement.
protected void outputDOMElement(Element element, OutputProcessorChain outputProcessorChain) throws XMLStreamException, XMLSecurityException {
NamedNodeMap namedNodeMap = element.getAttributes();
List<XMLSecAttribute> attributes = new ArrayList<>(namedNodeMap.getLength());
List<XMLSecNamespace> namespaces = new ArrayList<>(namedNodeMap.getLength());
for (int i = 0; i < namedNodeMap.getLength(); i++) {
Attr attribute = (Attr) namedNodeMap.item(i);
if (attribute.getPrefix() == null) {
attributes.add(createAttribute(new QName(attribute.getNamespaceURI(), attribute.getLocalName()), attribute.getValue()));
} else if ("xmlns".equals(attribute.getPrefix()) || "xmlns".equals(attribute.getLocalName())) {
namespaces.add(createNamespace(attribute.getLocalName(), attribute.getValue()));
} else {
attributes.add(createAttribute(new QName(attribute.getNamespaceURI(), attribute.getLocalName(), attribute.getPrefix()), attribute.getValue()));
}
}
QName elementName = new QName(element.getNamespaceURI(), element.getLocalName(), element.getPrefix());
createStartElementAndOutputAsEvent(outputProcessorChain, elementName, namespaces, attributes);
Node childNode = element.getFirstChild();
while (childNode != null) {
switch(childNode.getNodeType()) {
case Node.ELEMENT_NODE:
outputDOMElement((Element) childNode, outputProcessorChain);
break;
case Node.TEXT_NODE:
createCharactersAndOutputAsEvent(outputProcessorChain, ((Text) childNode).getData());
break;
}
childNode = childNode.getNextSibling();
}
createEndElementAndOutputAsEvent(outputProcessorChain, elementName);
}
Aggregations