⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlpolicyparser.java

📁 一个完整的XACML工程,学习XACML技术的好例子!
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    
    
    protected XMLPolicyParser(){}
    
    /**
     * This constructor can build a policy out of an InputSource, containing an
     * XML Policy.
     *
     * @param xmlSource is the source, containing the XML Policy to parse
     */
    public XMLPolicyParser(org.xml.sax.InputSource xmlSource) throws issrg.pba.PbaException{
        _init_(xmlSource);
    }
    
    /**
     * This constructor can build a policy out of the string representation of 
     * the XML.
     *
     * @param xml is the XML text of the policy
     */
    public XMLPolicyParser(String xml) throws issrg.pba.PbaException {
        org.xml.sax.InputSource xmlSource = new org.xml.sax.InputSource(new java.io.StringReader(xml));
        _init_(xmlSource);
    }
    
    /**
     * This is the method that does the actual initialisation of the object. It
     * accepts parameters in the same meaning as the constructor with the same
     * parameter.
     *
     * @param xmlSource is the source, containing the XML Policy to parse
     */
    private void _init_(org.xml.sax.InputSource xmlSource) throws issrg.pba.PbaException {
        SAXParserFactory sf = SAXParserFactory.newInstance(); //new SAXParserFactoryImpl();//newInstance();
        // perhaps, I'll remove this?..
        sf.setValidating(false);
        sf.setNamespaceAware(false);
        
        try{
            org.xml.sax.XMLReader parser = sf.newSAXParser().getXMLReader();
      /*parser.setEntityResolver(new org.xml.sax.EntityResolver(){
       *                //private final org.xml.sax.InputSource is = new org.xml.sax.InputSource(new java.io.FileReader("policy.dtd"));
       *                public org.xml.sax.InputSource resolveEntity(String id0, String id1){
       *                  try{
       *                    return new org.xml.sax.InputSource(new java.io.FileReader(id0==null?id1:id0));  // always return an empty Input Source
       *                  }catch(java.io.FileNotFoundException fnfe){
       *                    return null;
       *                  }
       *                }
       *              });
       */
            //parser.setErrorHandler(this);
            parser.setContentHandler(this);
            parser.parse(xmlSource);
            //sf.newSAXParser().parse(xmlSource, new org.xml.sax.helpers.DefaultHandler());
        }catch (org.xml.sax.SAXParseException se){
            if (se.getException()!=null){
                String cause=se.getCause().getMessage();
                throw new issrg.pba.PbaException("Could not initialise; parse error @ line "+se.getLineNumber(), se.getException());
                
            }else{
                String cause=se.getMessage();
                throw new issrg.pba.PbaException("Could not initialise; parse error @ line "+se.getLineNumber(), se);
                //String cause=se.getCause().getMessage();
            }
        }catch (org.xml.sax.SAXException sae){
            throw new issrg.pba.PbaException("Policy parse failed: "+sae.getMessage(), sae);
        }catch (javax.xml.parsers.ParserConfigurationException pce){
            throw new issrg.pba.PbaException("Policy parse failed: "+pce.getMessage(), pce);
        }catch (java.io.IOException io){
            throw new issrg.pba.PbaException("IO failure: "+io.getMessage(), io);
        }
    }
    
    /**
     * VOID
     */
    public void skippedEntity(String what){
    }
    
    /**
     * VOID
     */
    public void processingInstruction(String target, String data){
    }
    
    /**
     * VOID
     */
    public void ignorableWhitespace(char [] arr, int start, int len){
    }
    
    /**
     * This is a callback function that is invoked by the SAX parsing process. 
     * It will be 
     * invoked when the parse encounter string text in the xml file.
     *
     * <p>For the details of the parameters please refer to the XML SAX API 
     * documentation.
     *
     * @Gansen: I changed this implementation to allow the system to get the strings of obligations.
     * before I do this, the system only get the attributes of obligation.
     *
     * @Sassa: Why don't you create a String out of a char array and trim it,
     *  if you want to get rid of whitespace?
     */
    
    public void characters(char [] arr, int start, int len){
        if (len>0 && nodeStack.size()>0) // that's your test if it is inside a node - should always be true, because this method is called so; just a sanity check
          ((PolicyXMLNode)nodeStack.get(0)).addString(new String(arr, start, len).trim());
    }
    
    /**
     * This is the start of the recursion body of the parser; it is called any 
     * time
     * the opening tag of an element is encountered: <b>You should not invoke
     * it manually</b>. It creates a proper
     * PolicyXMLNode implementation object and maintains a stack of nodes for 
     * the
     * current branch.
     *
     * @see PolicyXMLNode
     */
    public void startElement(String URI, String localName, String qName, org.xml.sax.Attributes attrs) throws org.xml.sax.SAXParseException{
//    System.out.println("getting in: <"+localName+"("+qName+")"+"> @ "+locator.getLineNumber()+"..." ); //****
        //System.out.println("Attrs:" + attrs.getIndex(qName)+":" + attrs.getLocalName(0) + ":" + attrs.getQName(0) + ":" + attrs.getType(0) + ":" + attrs.getValue(0));
//    int num = attrs.getLength();
//    System.out.println(num);
//    for (int i = 0; i < num; i++) {
//        System.out.println("LocalName: " + attrs.getLocalName(i) + "Qname: " + attrs.getQName(i) + "Type: " + attrs.getType(i) + "URI: " + attrs.getURI(i) + "Value: " + attrs.getValue(i));
//    }
        
        //localName = localName.intern();
        if (localName.intern()==""){
            localName=qName;//.intern();
        }
        // from now on it is safe (and quick) to just compare localName to one of the *_NODE variables
        //System.out.print(localName+"..."); //****
        
        this.attrs=attrs;
        
        PolicyXMLNode node=null;
        try{
            java.lang.reflect.Constructor co = (java.lang.reflect.Constructor)knownNodes.get(localName);
            if (co!=null){  // if there is such node defined; otherwise it must be some node that does not have a corresponding class, so the PolicyXMLNode class should be used instead
                node=(PolicyXMLNode)(co.newInstance(new Object[]{this, this.attrs}));
            }
        }catch(Exception iae){
            throw new org.xml.sax.SAXParseException(iae.getMessage(), locator, iae);
            //iae.printStackTrace(); //****
            //node=null;
        }/*catch(InstantiationException ie){
      //ie.printStackTrace(); //****
      //node=null;
    }catch(java.lang.reflect.InvocationTargetException ite){
      //ite.printStackTrace(); //****
    }*/
        
        if (node==null){
            node = new PolicyXMLNode(localName, attrs);
        }
        nodeStack.insertElementAt(node, 0);
        
        //System.out.println("instantiated "+node.getClass().getName()+" ok"); //*****
    }
    
    /**
     * This is the end of the recursion body; it is called each time the element
     * closing tag is encountered: <b>You should not invoke
     * it manually</b>. This method simply maintains the node stack and 
     * constructs
     * the XML node tree, and invokes the <code>{@link PolicyXMLNode#construct()
     * construct}</code> method of the closing element.
     */
    public void endElement(String URI, String localName, String qName) throws org.xml.sax.SAXException {
        //System.out.print("getting out: </"+localName+"("+qName+")"+"> @ "+locator.getLineNumber()+"..."); //****
        
        try{
            PolicyXMLNode node = (PolicyXMLNode)nodeStack.remove(0);
            
            if (nodeStack.size()==0){  // we are there at last! :-)
                pmiXMLPolicy=(PMIXMLPolicyNode)node;
            }else{
                ((PolicyXMLNode)nodeStack.get(0)).addChild(node);
            }
            
            // now let the node know it finished and is ready to prepare its data now
            //System.out.print("entering the construct..."); //*******
            node.construct();
            //System.out.println("ok"); //*******
        }catch (Exception e){
            throw new org.xml.sax.SAXParseException(e.getMessage(), locator, e);
        }catch (Throwable th){
            throw new org.xml.sax.SAXParseException(th.getMessage(), locator);
        }
    }
    
    public void startDocument(){
        pmiXMLPolicy = null;
        roleHierarchyPolicy = null;
        nodeStack = new java.util.Vector();
    }
    
    /**
     * VOID
     */
    public void endDocument(){
    }
    
    /**
     * This method sets a source locator, which is used when sending error
     * messages: <b>You should not invoke this method manually</b>.
     */
    public void setDocumentLocator(org.xml.sax.Locator l){
        this.locator=l;
    }
    
    /**
     * VOID
     */
    public void startPrefixMapping(String prefix, String URI){
    }
    
    /**
     * VOID
     */
    public void endPrefixMapping(String prefix){
    }
    
    
  /*
   * PARSER FUNCTIONS ENDED.
   */
    
  /*
   * ERROR HANDLER FUNCTIONS
   */
    public void warning(org.xml.sax.SAXParseException spe){
    }
    
    public void fatalError(org.xml.sax.SAXParseException spe){
    }
    
    public void error(org.xml.sax.SAXParseException spe){
    }
    
    /**
     * Does the same as expect with boolean parameter optional=false.
     *
     * @see expect(java.util.Vector, int, String, boolean)
     */
    
    private static PolicyXMLNode expect(java.util.Vector where, int pos, String what) throws PolicyParsingException{
        return expect(where, pos, what, false);
    }
    
    /**
     * This is a tiny service function that just checks if the given vector of 
     * child
     * nodes contains the needed node at the given position. If not, an 
     * exception
     * is thrown. It may be marked optional, in which case null is returned if 
     * the
     * node at that position is not what is expected.
     *
     * @param where is the vector of child nodes
     * @param pos is the position in the vector where the sought node is 
     *      expected to be
     * @param what is the string name of the node
     * @param optional says whether the sought node is optional or not; if true, 
     *      no
     *      exception is thrown if the node is not what was expected, or the 
     *      vector
     *      is too short
     *
     * @return the reference to the node
     *
     * @throws PolicyParsingException if the vector is too short or the node in 
     *      that
     *      position is not the expected one
     */
    
    private static PolicyXMLNode expect(java.util.Vector where, int pos, String what, boolean optional) throws PolicyParsingException{
        try{
            if (where.size()<=pos){
              if (!optional) throw new PolicyParsingException("Cannot access an expected child node: "+what);
              return null;  // if optional, then just return null
            }
            PolicyXMLNode c2 = (PolicyXMLNode)where.get(pos);
            if (c2.getName().intern()!=what.intern()){
                if (!optional) throw new PolicyParsingException(what+" node has been exected, but "+c2.getName()+" was found");
                c2=null;
            }
            
            return c2;
        }catch (PolicyParsingException ppe){
            throw ppe;    // don't embed the PPE.
        }catch (Throwable th){
            // could be a class cast exception, for example
            throw new PolicyParsingException("Parse error", th);
        }
    }
    
    
  /*
   * POLICY FUNCTIONS
   */
    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -