17 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
|
|
4 |
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
|
|
|
5 |
*
|
|
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
|
|
7 |
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
|
|
|
8 |
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
|
|
|
9 |
* language governing permissions and limitations under the License.
|
|
|
10 |
*
|
|
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
package org.openconcerto.xml;
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* @author Sylvain CUAZ
|
|
|
18 |
*/
|
|
|
19 |
public final class XPathUtils {
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Compute the parent of a path. Note : path must lead to an element.
|
|
|
23 |
*
|
|
|
24 |
* @param path the path with at least one slash, eg "./elem".
|
|
|
25 |
* @return the parent of path, eg ".".
|
|
|
26 |
*/
|
|
|
27 |
public final static String parentOf(String path) {
|
|
|
28 |
return path.substring(0, path.lastIndexOf('/'));
|
|
|
29 |
}
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Compute the name of the last part of the path.
|
|
|
33 |
*
|
|
|
34 |
* @param path the path, eg "./elem" or "elem".
|
|
|
35 |
* @return the name, eg "elem".
|
|
|
36 |
*/
|
|
|
37 |
public final static String basename(String path) {
|
|
|
38 |
return path.substring(path.lastIndexOf('/') + 1);
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Compute the namespace prefix of a qualified name. NOTE : this also works if qName is a path,
|
|
|
43 |
* eg "./office:body/office:text".
|
|
|
44 |
*
|
|
|
45 |
* @param qName a qualified name, eg "office:text".
|
|
|
46 |
* @return the prefix or <code>null</code> if there's none, eg "office".
|
|
|
47 |
*/
|
|
|
48 |
public final static String namespace(String qName) {
|
|
|
49 |
qName = basename(qName);
|
180 |
ilm |
50 |
return XMLUtils.splitQualifiedName(qName).get0();
|
17 |
ilm |
51 |
}
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Compute the local name of a qualified name. NOTE : this also works if qName is a path, eg
|
|
|
55 |
* "./office:body/office:text" or if there's no prefix, eg "./office:body/child".
|
|
|
56 |
*
|
|
|
57 |
* @param qName a qualified name, eg "office:text".
|
|
|
58 |
* @return the local name, eg "text".
|
|
|
59 |
*/
|
|
|
60 |
public final static String localName(String qName) {
|
|
|
61 |
qName = basename(qName);
|
180 |
ilm |
62 |
return XMLUtils.splitQualifiedName(qName).get1();
|
17 |
ilm |
63 |
}
|
|
|
64 |
|
|
|
65 |
private XPathUtils() {
|
|
|
66 |
// static only
|
|
|
67 |
}
|
|
|
68 |
}
|