package org.objectweb.proactive.extensions.gcmdeployment.environment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.TransformerException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;

import org.objectweb.proactive.core.xml.VariableContractImpl;
import org.objectweb.proactive.extensions.gcmdeployment.GCMParserHelper;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;


public class Environment {

    public static InputSource replaceVariables(File descriptor, VariableContractImpl vContract, XPath xpath,
            String namespace) throws IOException, SAXException, XPathExpressionException,
            TransformerException {

        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true);
        domFactory.setIgnoringComments(true);

        // Get the variable map
        EnvironmentParser environmentParser;
        Map<String, String> variableMap;
        environmentParser = new EnvironmentParser(descriptor, vContract, domFactory, xpath, namespace);
        variableMap = environmentParser.getVariableMap();

        DocumentBuilder newDocumentBuilder = GCMParserHelper.getNewDocumentBuilder(domFactory);

        Document baseDocument = newDocumentBuilder.parse(descriptor);

        EnvironmentTransformer environmentTransformer;
        environmentTransformer = new EnvironmentTransformer(variableMap, baseDocument);

        File tempFile = File.createTempFile(descriptor.getName(), null);
        OutputStream outputStream = new FileOutputStream(tempFile);
        environmentTransformer.transform(outputStream);
        outputStream.close();

        InputSource inputSource = new InputSource(new FileInputStream(tempFile));
        return inputSource;
    }

}

@@ -581,43 +581,177 @@
 rser
-Helper;%0Aimport org.w3c.dom.Document
+Constants;%0Aimport org.objectweb.proactive.extensions.gcmdeployment.GCMParserHelper;%0Aimport org.w3c.dom.Document;%0Aimport org.w3c.dom.NamedNodeMap;%0Aimport org.w3c.dom.Node
 ;%0Aim
@@ -1696,24 +1696,1217 @@
 scriptor);%0A%0A
+        // sanity check on the document's namespace%0A        // we have to do this because we have no schema validation at this stage %0A        //%0A        String expectedNamespace = namespace.equals(GCMParserConstants.GCM_APPLICATION_NAMESPACE_PREFIX) ? GCMParserConstants.GCM_APPLICATION_NAMESPACE%0A                : GCMParserConstants.GCM_DEPLOYMENT_NAMESPACE;%0A        NamedNodeMap rootNodeAttributes = baseDocument.getFirstChild().getAttributes();%0A        if (rootNodeAttributes != null) %7B%0A            Node attr = rootNodeAttributes.getNamedItem(%22xmlns%22);%0A            if (attr == null %7C%7C !attr.getNodeValue().equals(expectedNamespace)) %7B%0A                if (attr != null && attr.getNodeValue().equals(%22urn:proactive:deployment:3.3%22)) %7B%0A                    throw new SAXException(%22descriptor is using old format - expected namespace is %22 +%0A                        expectedNamespace);%0A                %7D else %7B%0A                    throw new SAXException(%22document has wrong namespace or no namespace - must be in %22 +%0A                        expectedNamespace);%0A                %7D%0A            %7D%0A        %7D else %7B%0A            throw new SAXException(%22couldn't check document's namespace%22);%0A        %7D%0A%0A
         Envi

Fix for PROACTIVE-245 : put basic check on namespace back. Made error message a bit more friendly so it would tell when the descriptor is detected as being in the old format.