You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commons-dev@ws.apache.org by we...@apache.org on 2006/02/20 13:24:10 UTC

svn commit: r379115 - /webservices/commons/modules/policy/examples/wsse/src/secParser/WSSPolicyPrinter.java

Author: werner
Date: Mon Feb 20 04:24:02 2006
New Revision: 379115

URL: http://svn.apache.org/viewcvs?rev=379115&view=rev
Log:
A small utility that take policies, normalizes and merges them and produces a pretty printing.
This utility needs Xerces - pls update your classpath.

Added:
    webservices/commons/modules/policy/examples/wsse/src/secParser/WSSPolicyPrinter.java

Added: webservices/commons/modules/policy/examples/wsse/src/secParser/WSSPolicyPrinter.java
URL: http://svn.apache.org/viewcvs/webservices/commons/modules/policy/examples/wsse/src/secParser/WSSPolicyPrinter.java?rev=379115&view=auto
==============================================================================
--- webservices/commons/modules/policy/examples/wsse/src/secParser/WSSPolicyPrinter.java (added)
+++ webservices/commons/modules/policy/examples/wsse/src/secParser/WSSPolicyPrinter.java Mon Feb 20 04:24:02 2006
@@ -0,0 +1,141 @@
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package secParser;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ByteArrayInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+
+import org.apache.ws.policy.Policy;
+import org.apache.ws.policy.util.PolicyReader;
+import org.apache.ws.policy.util.PolicyWriter;
+import org.apache.ws.policy.util.PolicyFactory;
+
+import java.math.*;
+import org.w3c.dom.*;
+import org.xml.sax.SAXException;
+import javax.xml.parsers.*;
+import org.apache.xml.serialize.*;
+
+/**
+ * @author Werner Dittmann (werner@apache.org)
+ */
+
+public class WSSPolicyPrinter {
+
+    FileInputStream fis = null;
+
+    PolicyReader prdr = null;
+
+    PolicyWriter prwrt = null;
+
+    Policy merged = null;
+
+    public static void main(String[] args) throws Exception {
+
+        WSSPolicyPrinter processor = new WSSPolicyPrinter();
+        if (!processor.setup()) {
+            return;
+        }
+        String[] files = new String[1];
+        files = new String[2];
+        files[0] = "policy/examples/wsse/resources/SecurityPolicyBindingsAsymmTest.xml";
+        files[1] = "policy/examples/wsse/resources/SecurityPolicyMsg.xml";
+        processor.go(files);
+    }
+
+    boolean setup() throws NoSuchMethodException {
+        prdr = PolicyFactory.getPolicyReader(PolicyFactory.OM_POLICY_READER);
+        prwrt = PolicyFactory.getPolicyWriter(PolicyFactory.StAX_POLICY_WRITER);
+
+        return true;
+    }
+
+    void go(String[] args) {
+
+        merged = null;
+        for (int i = 0; i < args.length; i++) {
+            try {
+                System.out.println(new File("").getAbsolutePath());
+                fis = new FileInputStream(args[i]);
+            } catch (FileNotFoundException e) {
+                // TODO Auto-generated catch block
+                e.printStackTrace();
+            }
+
+            Policy newPolicy = prdr.readPolicy(fis);
+            newPolicy = (Policy) newPolicy.normalize();
+
+            if (merged == null) {
+                merged = newPolicy;
+            } else {
+                merged = (Policy) merged.merge(newPolicy);
+            }
+            try {
+                fis.close();
+            } catch (IOException e) {
+                // TODO Auto-generated catch block
+                e.printStackTrace();
+            }
+        }
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        prwrt.writePolicy(merged, baos);
+        prettyPrint(baos);
+    }
+
+    void prettyPrint(ByteArrayOutputStream baos) {
+
+        ByteArrayInputStream stream = new ByteArrayInputStream(baos
+                .toByteArray());
+
+        try {
+
+            // Find the implementation
+            DocumentBuilderFactory factory = DocumentBuilderFactory
+                    .newInstance();
+            factory.setNamespaceAware(true);
+            DocumentBuilder builder = factory.newDocumentBuilder();
+
+            // Create the document
+            Document doc = builder.parse(stream);
+
+            // Serialize the document
+            OutputFormat format = new OutputFormat(doc);
+            format.setLineWidth(80);
+            format.setIndenting(true);
+            format.setIndent(2);
+            XMLSerializer serializer = new XMLSerializer(System.out, format);
+            serializer.serialize(doc);
+
+        } catch (FactoryConfigurationError e) {
+            System.out.println("Could not locate a JAXP factory class");
+        } catch (ParserConfigurationException e) {
+            System.out.println("Could not locate a JAXP DocumentBuilder class");
+        } catch (DOMException e) {
+            System.err.println(e);
+        } catch (IOException e) {
+            System.err.println(e);
+        } catch (SAXException e) {
+            System.err.println(e);
+        }
+
+    }
+
+}