/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with this
 * work for additional information regarding copyright ownership. The ASF
 * licenses this file to You 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.
 */
package org.apache.sling.servlets.post.impl.helper;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.ValueFactory;

/**
 * Takes a string representation of a node (either a path or a uuid) and tries for parse it.
 */
public class ReferenceParser {

    private final Session session;

    public ReferenceParser(Session session) {
        this.session = session;
    }

    /**
     * Parses the given source string and returns the corresponding node.
     * If no node matches returns <code>null</code>.
     * <p/>
     *
     * @param value a path or UUID
     * @return the node or <code>null</code>
     * @throws RepositoryException
     */
    public Node parse(String value) throws RepositoryException {
        boolean valueIsNodePath = false;
        try {
            if (session.itemExists(value)) {
                valueIsNodePath = true;
            }
        } catch (RepositoryException e) {}
        if (valueIsNodePath) {
            return (Node) session.getItem(value);
        } else {
            return session.getNodeByUUID(value);
        }
    }

    /**
     * Parses the given source strings and returns the respective jcr Node value
     * instances. If no node matches for any of the sources
     * returns <code>null</code>.
     * <p/>
     *
     * @param values path or UUID strings
     * @param factory the value factory
     * @return the nodes or <code>null</code>
     * @throws RepositoryException
     */
    public Value[] parse(String[] values, ValueFactory factory) throws RepositoryException {
        Value ret[] = new Value[values.length];
        for (int i=0; i< values.length; i++) {
            Node n = parse(values[i]);
            if (n == null) {
                return null;
            }
            ret[i] = factory.createValue(n);
        }
        return ret;
    }

}

@@ -1585,49 +1585,8 @@
 n %7B%0A
-        boolean valueIsNodePath = false;%0A
     
@@ -1660,30 +1660,44 @@
     
-valueIsNodePath = tr
+return (Node) session.getItem(val
 ue
+)
 ;%0A  
@@ -1749,13 +1749,17 @@
 ion 
+ignor
 e) %7B
-%7D
 %0A   
@@ -1767,28 +1767,51 @@
     
-if (valueIsNodePath)
+    // we ignore this%0A        %7D%0A        try
  %7B%0A 
@@ -1819,39 +1819,32 @@
           return
- (Node)
  session.getItem
@@ -1831,36 +1831,42 @@
 turn session.get
-Item
+NodeByUUID
 (value);%0A       
@@ -1872,73 +1872,105 @@
   %7D 
-else %7B%0A            return session.getNodeByUUID(value);%0A        %7D
+catch (RepositoryException ignore) %7B%0A            // we ignore this%0A        %7D%0A        return null;
 %0A   

Make parse method match its javadoc