4 Replies Latest reply on Aug 25, 2004 8:40 PM by ah

    XML Parser cannot parse Files with spaces in their names

    S S Newbie

      Here is my jsp code

      <%@ page import="java.net.URLDecoder,org.w3c.dom.Document,java.io.*,javax.xml.parsers.DocumentBuilder, javax.xml.parsers.DocumentBuilderFactory" %>
      <%

      try {
      //create a factory instance
      DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

      DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
      File f = new File(URLDecoder.decode("C:\\jboss-3.2.4\\server\\default\\deploy\\cm.ear\\cm.war\\html\\Rports\\Gland 5.xml"));
      Document oDocument = dBuilder.parse(f);
      }
      catch(Exception e) {
      e.printStackTrace();
      }

      %>

      The above code gives my FileNotFoundException. It does not like the space in the file name. If i rename my file and change the filename in the above code to Gland5.xml everything is fine. Same thing happens if I remove the decode call also. Can someone please help
      Thanks

        • 1. Re: XML Parser cannot parse Files with spaces in their names
          dannyyates Newbie

          Why are you using the URLDecoder? URLs can't have spaces in, and the string you are passing (apart from having a space in) is not a valid URL.

          Can't you just construct the File object direct from the String filename?

          • 2. Re: XML Parser cannot parse Files with spaces in their names
            S S Newbie

            Yes, even if i try

            File f =
            new File("C:\\jboss-3.2.4\\server\\default\\deploy\\cm.ear\\cm.war\\htm\\Rep\\Gland 5.xml");
            boolean b = f.exists();
            System.out.println("EXISTS" + b);
            Document oDocument = dBuilder.parse(f);

            I still get a problem. In the above case b = true. But the Xerces parser I believe has a problem.

            2004-08-05 13:46:35,422 INFO [STDOUT] EXISTStrue
            2004-08-05 13:46:35,432 INFO [STDOUT] java.io.FileNotFoundException: C:\jboss-3.2.4\server\default\deploy\cm.ear\cm.war\htm\Rep\Gland%205.xml
            2004-08-05 13:46:35,432 INFO [STDOUT] at org.jboss.net.protocol.file.FileURLConnection.connect(FileURLConnection.java:71)
            2004-08-05 13:46:35,432 INFO [STDOUT] at org.jboss.net.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:80)
            2004-08-05 13:46:35,432 INFO [STDOUT] at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
            2004-08-05 13:46:35,432 INFO [STDOUT] at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
            2004-08-05 13:46:35,432 INFO [STDOUT] at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
            2004-08-05 13:46:35,432 INFO [STDOUT] at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
            2004-08-05 13:46:35,432 INFO [STDOUT] at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
            2004-08-05 13:46:35,442 INFO [STDOUT] at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
            2004-08-05 13:46:35,442 INFO [STDOUT] at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
            2004-08-05 13:46:35,442 INFO [STDOUT] at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:161)


            The problem goes away if i have a file without any spaces in it

            • 3. Re: XML Parser cannot parse Files with spaces in their names
              dannyyates Newbie

              Interesting. This does appear to be a bug in your parser. Have you tried the parse(InputStream) method?

              • 4. Re: XML Parser cannot parse Files with spaces in their names
                ah Newbie

                2004-08-05 13:46:35,432 INFO [STDOUT] java.io.FileNotFoundException: C:\jboss-3.2.4\server\default\deploy\cm.ear\cm.war\htm\Rep\Gland%205.xml
                2004-08-05 13:46:35,432 INFO [STDOUT] at org.jboss.net.protocol.file.FileURLConnection.connect(FileURLConnection.java:71)
                2004-08-05 13:46:35,432 INFO [STDOUT] at org.jboss.net.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:80)
                2004-08-05 13:46:35,432 INFO [STDOUT] at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
                2004-08-05 13:46:35,432 INFO [STDOUT] at

                Here is part of XMLEntityManager.java code in XML Parser,

                URL location = new URL(expandedSystemId);
                URLConnection connect = location.openConnection();
                stream = connect.getInputStream();

                XML Parser actually passes the "correct encoded URI" string - expandedSystemId - to URL constructor. In Jboss's FileURLConnection.java, it does not properly decode this input URI string. That is why java.io.FileNotFoundException occurs when connect.getInputStream(); is called. I think it should be fixed in jboss-common.jar instead of XML Parser.