001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Alexandre Russel
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.annotations.gwt.client.util;
021
022import com.google.gwt.dom.client.Document;
023import com.google.gwt.dom.client.Node;
024
025/**
026 * @author <a href="mailto:arussel@nuxeo.com">Alexandre Russel</a>
027 */
028public class StringRangeXPointer implements XPointer {
029    private final XPathUtil pathUtil = new XPathUtil();
030
031    private final String url;
032
033    private final String path;
034
035    private final int startOffset;
036
037    private final int length;
038
039    private String xpointerString;
040
041    public StringRangeXPointer(String xpointer) {
042        xpointerString = xpointer;
043        this.url = xpointer.substring(0, xpointer.indexOf("#"));
044        xpointer = xpointer.replaceFirst(".*string-range\\(", "");
045        xpointer = xpointer.replaceFirst("\\)\\)$", "");
046        String[] args = xpointer.split(",");
047        this.path = args[0];
048        this.startOffset = Integer.parseInt(args[2].trim());
049        this.length = Integer.parseInt(args[3].trim());
050    }
051
052    public int getLength() {
053        return length;
054    }
055
056    public String getMethod() {
057        return "string-range";
058    }
059
060    public String getUrl() {
061        return url;
062    }
063
064    public String getXPath() {
065        return path;
066    }
067
068    public Node getFirstNode() {
069        return pathUtil.getNode(path, Document.get()).get(0);
070    }
071
072    public int getStartOffset() {
073        return startOffset;
074    }
075
076    public Document getOwnerDocument() {
077        return Document.get();
078    }
079
080    public String getXpointerString() {
081        return xpointerString;
082    }
083
084    @Override
085    public boolean equals(Object obj) {
086        if (!(obj instanceof StringRangeXPointer)) {
087            return false;
088        }
089        StringRangeXPointer xp = (StringRangeXPointer) obj;
090        return xpointerString.equals(xp.xpointerString);
091    }
092
093    @Override
094    public int hashCode() {
095        int result = 17;
096        result += 17 * xpointerString.hashCode();
097        return result;
098    }
099
100}