/*
 * Copyright 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.
 *
 */
/*
 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
 */
/*
 * $Id$
 */
package org.jcp.xml.dsig.internal.dom;

import org.w3c.dom.Attr;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import org.apache.xml.security.Init;
import org.apache.xml.security.utils.IdResolver;
import org.apache.xml.security.utils.resolver.ResourceResolver;
import org.apache.xml.security.signature.XMLSignatureInput;

import javax.xml.crypto.*;
import javax.xml.crypto.dom.*;
import javax.xml.crypto.dsig.*;

/**
 * DOM-based implementation of URIDereferencer.
 *
 * @author Sean Mullan
 */
public class DOMURIDereferencer implements URIDereferencer {
    
    static final URIDereferencer INSTANCE = new DOMURIDereferencer();

    private DOMURIDereferencer() {
	// need to call org.apache.xml.security.Init.init() 
        // before calling any apache security code
	Init.init();
    }

    public Data dereference(URIReference uriRef, XMLCryptoContext context)
	throws URIReferenceException {

	if (uriRef == null) {
	    throw new NullPointerException("uriRef cannot be null");
	}
	if (context == null) {
	    throw new NullPointerException("context cannot be null");
	}

	DOMURIReference domRef = (DOMURIReference) uriRef;
        Attr uriAttr = (Attr) domRef.getHere();
	String uri = uriRef.getURI();
        DOMCryptoContext dcc = (DOMCryptoContext) context;

	// Check if same-document URI and register ID
	if (uri != null && uri.length() != 0 && uri.charAt(0) == '#') {
            String id = uri.substring(1);

	    if (id.startsWith("xpointer(id(")) {
	        int i1 = id.indexOf('\'');
	        int i2 = id.indexOf('\'', i1+1);
		id = id.substring(i1+1, i2);
	    }

            // this is a bit of a hack to check for registered 
	    // IDRefs and manually register them with Apache's IdResolver 
	    // map which includes builtin schema knowledge of DSig/Enc IDs
	    if (context instanceof XMLSignContext) {
	        Node referencedElem = dcc.getElementById(id);
	        if (referencedElem != null) {
	            IdResolver.registerElementById((Element) referencedElem, id);
	        }
	    }
	} 

        try {
	    String baseURI = context.getBaseURI();
            ResourceResolver apacheResolver = 
	        ResourceResolver.getInstance(uriAttr, baseURI);
            XMLSignatureInput in = apacheResolver.resolve(uriAttr, baseURI);
	    if (in.isOctetStream()) {
	        return new ApacheOctetStreamData(in);
	    } else {
	        return new ApacheNodeSetData(in);
	    }
        } catch (Exception e) {
            throw new URIReferenceException(e);
        }
    }
}

@@ -2501,55 +2501,12 @@
 IDs%0A
-%09
     
-if (context instanceof XMLSignContext) %7B%0A%09
     
@@ -2560,20 +2560,16 @@
 );%0A%09    
-    
 if (refe
@@ -2599,20 +2599,16 @@
         
-    
 IdResolv
@@ -2665,19 +2665,8 @@
 d);%0A
-%09        %7D%0A
 %09   

Fix bug 39273: JSR 105 DOMCryptoContext.setIdAttributeNS not working when validating signatures