Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 Copyright 2014 Universidad Politcnica de Madrid - Center for Open Middleware (http://www.centeropenmiddleware.com)
    
 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.
 */

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    /**
     * Given a path of the form <code>{doc|document}('<i>aDoc.xml</i>')<i>/aPath</i></code>, it divides it into the doc and the path
     * @param pathWithDoc a path starting with the XPath 'doc' function or the XSLT 'document' function.
     * @return a string array such that array[0]=<i>docPath</i>, array[1]=<i>path</i> and array[2]=<i>pathWithoutRootElement</i> 
     */
    public static String[] divideDocAndPath(String pathWithDoc) {
        String[] result = new String[3];
        final String REGEXP_SEARCH = "^(doc|document)\\('(?<docName>.+)'\\)(?<path>/[^\\[\\]/]+(\\[.+\\])?(?<pathWithoutRoot>/.+))$";
        Pattern patternToSearch = Pattern.compile(REGEXP_SEARCH);
        Matcher matcher = patternToSearch.matcher(pathWithDoc);
        matcher.matches();
        result[0] = matcher.group("docName");
        result[1] = matcher.group("path");
        result[2] = matcher.group("pathWithoutRoot");
        return result;
    }
}