code: private static PolicyNodeImpl removeInvalidNodes(PolicyNodeImpl rootNode,
          int certIndex, Set<String> initPolicies,
          CertificatePoliciesExtension currCertPolicies)
          throws CertPathValidatorException
      {
          List<PolicyInformation> policyInfo = null;
          try {
              policyInfo = currCertPolicies.get(CertificatePoliciesExtension.POLICIES);
          } catch (IOException ioe) {
              throw new CertPathValidatorException("Exception while "
                  + "retrieving policyOIDs", ioe);
          }
  
          boolean childDeleted = false;
          for (PolicyInformation curPolInfo : policyInfo) {
              String curPolicy =
                  curPolInfo.getPolicyIdentifier().getIdentifier().toString();
  
              if (debug != null)
                  debug.println("PolicyChecker.processPolicies() "
                                + "processing policy second time: " + curPolicy);
  
              Set<PolicyNodeImpl> validNodes =
                          rootNode.getPolicyNodesValid(certIndex, curPolicy);
              for (PolicyNodeImpl curNode : validNodes) {
                  PolicyNodeImpl parentNode = (PolicyNodeImpl)curNode.getParent();
                  if (parentNode.getValidPolicy().equals(ANY_POLICY)) {
                      if ((!initPolicies.contains(curPolicy)) &&
                          (!curPolicy.equals(ANY_POLICY))) {
                          if (debug != null)
                              debug.println("PolicyChecker.processPolicies() "
                                  + "before deleting: policy tree = " + rootNode);
                          parentNode.deleteChild(curNode);
                          childDeleted = true;
                          if (debug != null)
                              debug.println("PolicyChecker.processPolicies() "
                                  + "after deleting: policy tree = " + rootNode);
                      }
                  }
              }
          }
  
          if (childDeleted) {
              rootNode.prune(certIndex);
              if (!rootNode.getChildren().hasNext()) {
                  rootNode = null;
              }
          }
  
          return rootNode;
      }
docs: Removes those nodes which do not intersect with the initial policies
  specified by the user.
  
  @param rootNode the root node of the valid policy tree
  @param certIndex the index of the certificate being processed
  @param initPolicies the Set of policies required by the user
  @param currCertPolicies the CertificatePoliciesExtension of the
  certificate being processed
  @returns the root node of the valid policy tree after modification
  @exception CertPathValidatorException Exception thrown if error occurs.