001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *        http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 */
019
020package org.apache.isis.core.runtime.snapshot;
021
022import org.w3c.dom.Document;
023import org.w3c.dom.Element;
024
025/**
026 * Stateless utility methods for manipulating XML documents.
027 */
028public final class Helper {
029
030    /**
031     * Helper method
032     */
033    String trailingSlash(final String str) {
034        return str.endsWith("/") ? str : str + "/";
035    }
036
037    /**
038     * Utility method that returns just the class's name for the supplied fully
039     * qualified class name.
040     * 
041     * cf 'basename' in Unix.
042     */
043    String classNameFor(final String fullyQualifiedClassName) {
044        final int fullNameLastPeriodIdx = fullyQualifiedClassName.lastIndexOf('.');
045        if (fullNameLastPeriodIdx > 0 && fullNameLastPeriodIdx < fullyQualifiedClassName.length()) {
046            return fullyQualifiedClassName.substring(fullNameLastPeriodIdx + 1);
047        } else {
048            return fullyQualifiedClassName;
049        }
050    }
051
052    /**
053     * Utility method that returns the package name for the supplied fully
054     * qualified class name, or <code>default</code> if the class is in no
055     * namespace / in the default namespace.
056     * 
057     * cf 'dirname' in Unix.
058     */
059    String packageNameFor(final String fullyQualifiedClassName) {
060        final int fullNameLastPeriodIdx = fullyQualifiedClassName.lastIndexOf('.');
061        if (fullNameLastPeriodIdx > 0) {
062            return fullyQualifiedClassName.substring(0, fullNameLastPeriodIdx);
063        } else {
064            return "default"; // TODO: should provide a better way to specify
065                              // namespace.
066        }
067    }
068
069    /**
070     * Returns the root element for the element by looking up the owner document
071     * for the element, and from that its document element.
072     * 
073     * If no document element exists, just returns the supplied document.
074     */
075    Element rootElementFor(final Element element) {
076        final Document doc = element.getOwnerDocument();
077        if (doc == null) {
078            return element;
079        }
080        final Element rootElement = doc.getDocumentElement();
081        if (rootElement == null) {
082            return element;
083        }
084        return rootElement;
085    }
086
087    Document docFor(final Element element) {
088        return element.getOwnerDocument();
089    }
090
091}