001/*
002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Alexandre Russel
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.annotations.gwt.client.util;
023
024import com.google.gwt.dom.client.Document;
025import com.google.gwt.dom.client.Node;
026
027/**
028 * @author <a href="mailto:arussel@nuxeo.com">Alexandre Russel</a>
029 */
030public class StringRangeXPointer implements XPointer {
031    private final XPathUtil pathUtil = new XPathUtil();
032
033    private final String url;
034
035    private final String path;
036
037    private final int startOffset;
038
039    private final int length;
040
041    private String xpointerString;
042
043    public StringRangeXPointer(String xpointer) {
044        xpointerString = xpointer;
045        this.url = xpointer.substring(0, xpointer.indexOf("#"));
046        xpointer = xpointer.replaceFirst(".*string-range\\(", "");
047        xpointer = xpointer.replaceFirst("\\)\\)$", "");
048        String[] args = xpointer.split(",");
049        this.path = args[0];
050        this.startOffset = Integer.parseInt(args[2].trim());
051        this.length = Integer.parseInt(args[3].trim());
052    }
053
054    public int getLength() {
055        return length;
056    }
057
058    public String getMethod() {
059        return "string-range";
060    }
061
062    public String getUrl() {
063        return url;
064    }
065
066    public String getXPath() {
067        return path;
068    }
069
070    public Node getFirstNode() {
071        return pathUtil.getNode(path, Document.get()).get(0);
072    }
073
074    public int getStartOffset() {
075        return startOffset;
076    }
077
078    public Document getOwnerDocument() {
079        return Document.get();
080    }
081
082    public String getXpointerString() {
083        return xpointerString;
084    }
085
086    @Override
087    public boolean equals(Object obj) {
088        if (!(obj instanceof StringRangeXPointer)) {
089            return false;
090        }
091        StringRangeXPointer xp = (StringRangeXPointer) obj;
092        return xpointerString.equals(xp.xpointerString);
093    }
094
095    @Override
096    public int hashCode() {
097        int result = 17;
098        result += 17 * xpointerString.hashCode();
099        return result;
100    }
101
102}