You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jackrabbit.apache.org by Thomas Frössman <" th...@jossystem.se> on 2006/11/01 22:03:56 UTC

Problems with character in file name

Hello, I am a newbie with a question.

My goal is to create a repository that mirrors a local filesystem, I 
have a problem with file names.
The problem is "This file will fail because of the ' character.txt".
The line that fails is
        Node fileNode = folderNode.addNode(file.getName(), "nt:file");

I have only tested this on  jdk build 1.6.0-rc-b103 on windows xp.

Exception in thread "main" javax.jcr.RepositoryException: failed to 
resolve path This file will fail because of the ' character.txt relative 
to /testdump4[2]: 'This file will fail because of the ' character.txt' 
is not a valid path: 'This file will fail because of the ' 
character.txt' is not a legal path element: 'This file will fail because 
of the ' character.txt' is not a valid path: 'This file will fail 
because of the ' character.txt' is not a legal path element
    at 
org.apache.jackrabbit.core.NodeImpl.internalAddNode(NodeImpl.java:712)
    at 
org.apache.jackrabbit.core.NodeImpl.internalAddNode(NodeImpl.java:687)
    at org.apache.jackrabbit.core.NodeImpl.addNode(NodeImpl.java:1925)
    at helloJackrabbit.Writer.importFile(Writer.java:106)
    at helloJackrabbit.Writer.getFileListing(Writer.java:89)
    at helloJackrabbit.Writer.main(Writer.java:50)
Caused by: org.apache.jackrabbit.name.MalformedPathException: 'This file 
will fail because of the ' character.txt' is not a valid path: 'This 
file will fail because of the ' character.txt' is not a legal path element
    at org.apache.jackrabbit.name.PathFormat.parse(PathFormat.java:191)
    at 
org.apache.jackrabbit.core.NodeImpl.internalAddNode(NodeImpl.java:698)
    ... 5 more
org.apache.jackrabbit.name.MalformedPathException: 'This file will fail 
because of the ' character.txt' is not a valid path: 'This file will 
fail because of the ' character.txt' is not a legal path element
    at org.apache.jackrabbit.name.PathFormat.parse(PathFormat.java:191)
    at 
org.apache.jackrabbit.core.NodeImpl.internalAddNode(NodeImpl.java:698)
    at 
org.apache.jackrabbit.core.NodeImpl.internalAddNode(NodeImpl.java:687)
    at org.apache.jackrabbit.core.NodeImpl.addNode(NodeImpl.java:1925)
    at helloJackrabbit.Writer.importFile(Writer.java:106)
    at helloJackrabbit.Writer.getFileListing(Writer.java:89)
    at helloJackrabbit.Writer.main(Writer.java:50)

****

*-
Thomas Frössman*: Intermedia production at Jossystem 
<http://www.jossystem.se> & Polorization <http://www.polorization.net>
contact | thomasf@jossystem.se <ma...@jossystem.se> - +46704666133

Re: Problems with character in file name

Posted by Jukka Zitting <ju...@gmail.com>.
Hi,

On 11/1/06, thomas-mlists"@jossystem.se Thomas Frössman <"> wrote:
> Hello, I am a newbie with a question.

Welcome!

> My goal is to create a repository that mirrors a local filesystem, I
> have a problem with file names.
> The problem is "This file will fail because of the ' character.txt".
> The line that fails is
>         Node fileNode = folderNode.addNode(file.getName(), "nt:file");

The set of valid JCR item names is a superset of valid XML names, but
a single quote is not allowed. See section 4.6 of the JCR
specification for the exact rules.

There is a utility class in Jackrabbit called
org.apache.jackrabbit.util.Text, that contains a number of escaping
and encoding methods for use with JCR. In your case I would suggest
you to use the Text.escapeIllegalJcrChars() and
Text.unescapeIllegalJcrChars() methods to make sure that you only
store valid JCR names. The methods use URI encoding but only where
absolutely necessary to keep the resulting name valid. Thus in normal
use the amount of encoded characters in names should be very small.

With the Text class your code would be:

    Node fileNode = folderNode.addNode(
       Text.escapeIllegalJcrChars(file.getName()), "nt:file");

Then, when passing the name back to a client, you'd do:

   String name = Text.unescapeIllegalJcrChars(fileNode.getName());

BR,

Jukka Zitting