in src/main/java/org/apache/xml/security/stax/impl/transformer/canonicalizer/CanonicalizerBase.java [255:384]
public void transform(final XMLSecEvent xmlSecEvent) throws XMLStreamException {
try {
OutputStream outputStream = getOutputStream(); //NOPMD
switch (xmlSecEvent.getEventType()) {
case XMLStreamConstants.START_ELEMENT:
final XMLSecStartElement xmlSecStartElement = xmlSecEvent.asStartElement();
currentDocumentLevel = DocumentLevel.NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT;
outputStack.push(Collections.<Comparable>emptyList());
final List<XMLSecNamespace> utilizedNamespaces;
final List<XMLSecAttribute> utilizedAttributes;
if (firstCall) {
utilizedNamespaces = new ArrayList<>();
utilizedAttributes = new ArrayList<>();
outputStack.peek().add(XMLSecEventFactory.createXMLSecNamespace(null, ""));
outputStack.push(Collections.<Comparable>emptyList());
utilizedNamespaces.addAll(getInitialUtilizedNamespaces(xmlSecStartElement, outputStack));
utilizedAttributes.addAll(getInitialUtilizedAttributes(xmlSecStartElement, outputStack));
firstCall = false;
} else {
utilizedNamespaces = getCurrentUtilizedNamespaces(xmlSecStartElement, outputStack);
utilizedAttributes = getCurrentUtilizedAttributes(xmlSecStartElement, outputStack);
}
outputStream.write('<');
final String prefix = xmlSecStartElement.getName().getPrefix();
if (prefix != null && !prefix.isEmpty()) {
UtfHelpper.writeByte(prefix, outputStream, CACHE);
outputStream.write(DOUBLEPOINT);
}
final String name = xmlSecStartElement.getName().getLocalPart();
UtfHelpper.writeByte(name, outputStream, CACHE);
if (!utilizedNamespaces.isEmpty()) {
Collections.sort(utilizedNamespaces);
for (final XMLSecNamespace xmlSecNamespace : utilizedNamespaces) {
if (!namespaceIsAbsolute(xmlSecNamespace.getNamespaceURI())) {
throw new XMLStreamException("namespace is relative encountered: " + xmlSecNamespace.getNamespaceURI());
}
if (xmlSecNamespace.isDefaultNamespaceDeclaration()) {
outputAttrToWriter(null, XMLNS, xmlSecNamespace.getNamespaceURI(), outputStream, CACHE);
} else {
outputAttrToWriter(XMLNS, xmlSecNamespace.getPrefix(), xmlSecNamespace.getNamespaceURI(), outputStream, CACHE);
}
}
}
if (!utilizedAttributes.isEmpty()) {
Collections.sort(utilizedAttributes);
for (final XMLSecAttribute xmlSecAttribute : utilizedAttributes) {
final QName attributeName = xmlSecAttribute.getName();
final String attributeNamePrefix = attributeName.getPrefix();
if (attributeNamePrefix != null && !attributeNamePrefix.isEmpty()) {
outputAttrToWriter(attributeNamePrefix, attributeName.getLocalPart(), xmlSecAttribute.getValue(), outputStream, CACHE);
} else {
outputAttrToWriter(null, attributeName.getLocalPart(), xmlSecAttribute.getValue(), outputStream, CACHE);
}
}
}
outputStream.write('>');
break;
case XMLStreamConstants.END_ELEMENT:
final XMLSecEndElement xmlSecEndElement = xmlSecEvent.asEndElement();
final String localPrefix = xmlSecEndElement.getName().getPrefix();
outputStream.write(_END_TAG);
if (localPrefix != null && !localPrefix.isEmpty()) {
UtfHelpper.writeByte(localPrefix, outputStream, CACHE);
outputStream.write(DOUBLEPOINT);
}
UtfHelpper.writeByte(xmlSecEndElement.getName().getLocalPart(), outputStream, CACHE);
outputStream.write('>');
//We finished with this level, pop to the previous definitions.
outputStack.pop();
if (outputStack.size() == 1) {
currentDocumentLevel = DocumentLevel.NODE_AFTER_DOCUMENT_ELEMENT;
}
break;
case XMLStreamConstants.PROCESSING_INSTRUCTION:
outputPItoWriter((XMLSecProcessingInstruction) xmlSecEvent, outputStream, currentDocumentLevel);
break;
case XMLStreamConstants.CHARACTERS:
if (currentDocumentLevel == DocumentLevel.NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT) {
outputTextToWriter(xmlSecEvent.asCharacters().getText(), outputStream);
}
break;
case XMLStreamConstants.COMMENT:
if (includeComments) {
outputCommentToWriter((XMLSecComment) xmlSecEvent, outputStream, currentDocumentLevel);
}
break;
case XMLStreamConstants.SPACE:
if (currentDocumentLevel == DocumentLevel.NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT) {
outputTextToWriter(xmlSecEvent.asCharacters().getText(), outputStream);
}
break;
case XMLStreamConstants.START_DOCUMENT:
currentDocumentLevel = DocumentLevel.NODE_BEFORE_DOCUMENT_ELEMENT;
break;
case XMLStreamConstants.END_DOCUMENT:
break;
case XMLStreamConstants.ENTITY_REFERENCE:
throw new XMLStreamException("illegal event :" + XMLSecurityUtils.getXMLEventAsString(xmlSecEvent));
case XMLStreamConstants.ATTRIBUTE:
throw new XMLStreamException("illegal event :" + XMLSecurityUtils.getXMLEventAsString(xmlSecEvent));
case XMLStreamConstants.DTD:
break;
case XMLStreamConstants.CDATA:
outputTextToWriter(xmlSecEvent.asCharacters().getData(), outputStream);
break;
case XMLStreamConstants.NAMESPACE:
throw new XMLStreamException("illegal event :" + XMLSecurityUtils.getXMLEventAsString(xmlSecEvent));
case XMLStreamConstants.NOTATION_DECLARATION:
throw new XMLStreamException("illegal event :" + XMLSecurityUtils.getXMLEventAsString(xmlSecEvent));
case XMLStreamConstants.ENTITY_DECLARATION:
throw new XMLStreamException("illegal event :" + XMLSecurityUtils.getXMLEventAsString(xmlSecEvent));
}
} catch (IOException e) {
throw new XMLStreamException(e);
}
}