001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
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: package org.outerj.daisy.repository.commonimpl;
017:
018: import org.outerj.daisy.util.Constants;
019: import org.outerj.daisy.repository.namespace.NamespaceNotFoundException;
020: import org.outerj.daisy.repository.namespace.NamespaceManager;
021: import org.outerj.daisy.repository.RepositoryRuntimeException;
022: import org.outerj.daisy.repository.Repository;
023: import org.outerj.daisy.repository.DocumentNotFoundException;
024: import org.outerj.daisy.repository.InvalidDocumentIdException;
025: import org.outerj.daisy.repository.commonimpl.namespace.CommonNamespaceManager;
026:
027: import java.util.regex.Matcher;
028:
029: /**
030: * Represents a parsed document ID. Only used in the internal implementation, not
031: * exposed to the outside world.
032: */
033: public final class DocId {
034: private final long docSeqId;
035: private final String namespace;
036: private final long nsId;
037: private final String docId;
038:
039: public DocId(long docSeqId, String namespace, long nsId) {
040: this .docSeqId = docSeqId;
041: this .namespace = namespace;
042: this .nsId = nsId;
043: this .docId = docSeqId + "-" + namespace;
044: }
045:
046: public long getSeqId() {
047: return docSeqId;
048: }
049:
050: public String getNamespace() {
051: return namespace;
052: }
053:
054: public long getNamespaceId() {
055: return nsId;
056: }
057:
058: /**
059: * Shorter name for {@link #getNamespaceId()}.
060: */
061: public long getNsId() {
062: return nsId;
063: }
064:
065: public String toString() {
066: return docId;
067: }
068:
069: public static DocId getDocId(long docSeqId, String namespace,
070: CommonRepository repository) {
071: long nsId;
072: try {
073: nsId = repository.getNamespaceManager().getNamespace(
074: namespace).getId();
075: } catch (NamespaceNotFoundException e) {
076: throw new RepositoryRuntimeException("Invalid namespace: "
077: + namespace, e);
078: }
079: return new DocId(docSeqId, namespace, nsId);
080: }
081:
082: public static DocId parseDocId(String documentId,
083: CommonRepository repository) {
084: try {
085: return parseDocId(documentId, repository, 1);
086: } catch (NamespaceNotFoundException e) {
087: throw new InvalidDocumentIdException(
088: "Invalid document ID \"" + documentId + "\".", e);
089: }
090: }
091:
092: public static DocId parseDocId(String documentId,
093: Repository repository) {
094: try {
095: return parseDocId(documentId, repository, 2);
096: } catch (NamespaceNotFoundException e) {
097: throw new InvalidDocumentIdException(
098: "Invalid document ID \"" + documentId + "\".", e);
099: }
100: }
101:
102: public static DocId parseDocIdThrowNotFound(String documentId,
103: CommonRepository repository)
104: throws DocumentNotFoundException {
105: try {
106: return parseDocId(documentId, repository, 1);
107: } catch (NamespaceNotFoundException e) {
108: throw new DocumentNotFoundException(documentId, e);
109: }
110: }
111:
112: public static DocId parseDocIdThrowNotFound(String documentId,
113: Repository repository) throws DocumentNotFoundException {
114: try {
115: return parseDocId(documentId, repository, 2);
116: } catch (NamespaceNotFoundException e) {
117: throw new DocumentNotFoundException(documentId, e);
118: }
119: }
120:
121: private static DocId parseDocId(String documentId,
122: Object repositoryObject, int repoType)
123: throws NamespaceNotFoundException {
124: if (documentId == null)
125: throw new IllegalArgumentException(
126: "documentId argument is not allowed to be null");
127: Matcher matcher = Constants.DAISY_COMPAT_DOCID_PATTERN
128: .matcher(documentId);
129: if (matcher.matches()) {
130: long docSeqId = Long.parseLong(matcher.group(1));
131: String namespace = matcher.group(2);
132: long nsId;
133: if (repoType == 1) {
134: CommonNamespaceManager namespaceManager = ((CommonRepository) repositoryObject)
135: .getNamespaceManager();
136: if (namespace == null)
137: namespace = namespaceManager
138: .getRepositoryNamespace();
139: nsId = namespaceManager.getNamespace(namespace).getId();
140: } else if (repoType == 2) {
141: NamespaceManager namespaceManager = ((Repository) repositoryObject)
142: .getNamespaceManager();
143: if (namespace == null)
144: namespace = namespaceManager
145: .getRepositoryNamespace();
146: nsId = namespaceManager.getNamespace(namespace).getId();
147: } else {
148: // assert check, should never occur
149: throw new RuntimeException(
150: "Unexpected repoType value: " + repoType);
151: }
152: return new DocId(docSeqId, namespace, nsId);
153: } else {
154: throw new InvalidDocumentIdException(
155: "Invalid document ID: \"" + documentId + "\".");
156: }
157: }
158:
159: public boolean equals(Object obj) {
160: DocId otherDocId = (DocId) obj;
161: return this.nsId == otherDocId.nsId
162: && this.docSeqId == otherDocId.docSeqId;
163: }
164: }
|