}
private void addPolicy(int type, PolicyInclude policyInclude,
OMElement element, OMFactory factory) throws Exception {
ArrayList elementList = policyInclude.getPolicyElements(type);
StAXPolicyWriter pwrt = (StAXPolicyWriter) PolicyFactory
.getPolicyWriter(PolicyFactory.StAX_POLICY_WRITER);
for (Iterator iterator = elementList.iterator(); iterator.hasNext();) {
Object policyElement = iterator.next();
if (policyElement instanceof Policy) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
pwrt.writePolicy((Policy) policyElement, baos);
ByteArrayInputStream bais = new ByteArrayInputStream(baos
.toByteArray());
element.addChild(OMXMLBuilderFactory.createStAXOMBuilder(
factory,
StAXUtils.createXMLStreamReader(
bais)).getDocumentElement());
} else if (policyElement instanceof PolicyReference) {
OMNamespace ns = factory.createOMNamespace(PolicyConstants.WS_POLICY_NAMESPACE_URI, PolicyConstants.WS_POLICY_PREFIX);
OMElement refElement = factory.createOMElement(PolicyConstants.WS_POLICY_REFERENCE, ns);
String policyURIString = ((PolicyReference) policyElement).getPolicyURIString();
OMAttribute attribute = factory.createOMAttribute("URI", null, policyURIString);
refElement.addAttribute(attribute);
element.addChild(refElement);
PolicyRegistry reg = policyInclude.getPolicyRegistry();
Policy p = reg.lookup(policyURIString);
if(p == null) {
throw new Exception("Policy not found for uri : " + policyURIString);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
pwrt.writePolicy(p, baos);
ByteArrayInputStream bais = new ByteArrayInputStream(baos
.toByteArray());
getDefinitionElement().addChild(OMXMLBuilderFactory.createStAXOMBuilder(
factory,