KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mapper > storage > RepositoryDocumentImpl


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 /*
24  * RepositoryDocument.java
25  *
26  * Created on 25 janvier 2002, 18:45
27  */

28 package org.xquark.mapper.storage;
29
30 import java.io.IOException JavaDoc;
31 import java.util.Date JavaDoc;
32
33 import org.xml.sax.SAXException JavaDoc;
34 import org.xquark.mapper.RepositoryDocument;
35 import org.xquark.mapper.RepositoryException;
36 import org.xquark.mapper.metadata.DocumentInfo;
37 import org.xquark.xml.xdbc.XMLDBCException;
38 import org.xquark.xml.xdbc.XMLDBCNotSupportedException;
39 import org.xquark.xquery.xdbc.AbstractXMLDocument;
40
41 /**
42  * Implementation of XMLDocument for document stored in the Repository.
43  *
44  */

45 public class RepositoryDocumentImpl extends AbstractXMLDocument
46 implements RepositoryDocument
47 {
48     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
49     private static final String JavaDoc RCSName = "$Name: $";
50     
51     private DocumentInfo docInfo;
52     
53     /** Creates a new instance of RepositoryDocument
54      * <p>Does not verify the document exists.</p>
55      */

56     public RepositoryDocumentImpl(String JavaDoc Id, XMLCollectionImpl collection)
57     {
58         super(Id, collection);
59     }
60     
61     /** Creates a new instance of RepositoryDocument with document information
62      * read from the database.
63      */

64     public RepositoryDocumentImpl (
65                                 DocumentInfo docInfo,
66                                 XMLCollectionImpl collection
67                                 )
68     {
69         this(docInfo.docID, collection);
70         setDocumentInfo(docInfo);
71     }
72     
73     public void setDocumentInfo(DocumentInfo docInfo)
74     {
75         this.docInfo = docInfo;
76     }
77     
78     ///////////////////////////////////////////////////////////////////////////
79
// XMLDocument implementation
80
///////////////////////////////////////////////////////////////////////////
81
public void setIdentifier(String JavaDoc identifier) throws XMLDBCException, XMLDBCNotSupportedException
82     {
83         super.setIdentifier(identifier);
84         getCollection().renameDocument(getIdentifier(), identifier); // Was it meant by the API ?
85
}
86     
87     public void remove() throws XMLDBCException, XMLDBCNotSupportedException
88     {
89         getCollection().removeDocument(getIdentifier());
90     }
91     
92     public void getAsSAX() throws XMLDBCException, SAXException JavaDoc
93     {
94         _RepositoryCollection collection = (_RepositoryCollection)getCollection();
95         RepositoryReader reader = collection.getReader();
96         plugHandlers(reader);
97         try
98         {
99             reader.parse(getIdentifier());
100         }
101         catch (IOException JavaDoc e)
102         {
103             // TO IMPROVE
104
throw new RepositoryException(RepositoryException.DB_ERROR, "IOException while reading document.");
105         }
106         collection.releaseReader(reader);
107     }
108     
109     ///////////////////////////////////////////////////////////////////////////
110
// RepositoryDocument implementation
111
///////////////////////////////////////////////////////////////////////////
112
public int getAverageNodeDataLength() throws XMLDBCException
113     {
114         if (docInfo == null)
115             getDocumentInfo();
116         return docInfo.avgNodeLen;
117     }
118     
119     public Date JavaDoc getCreationDate() throws XMLDBCException
120     {
121         if (docInfo == null)
122             getDocumentInfo();
123         return docInfo.creationDate;
124      }
125     
126     public long getEstimatedDocumentLength() throws XMLDBCException
127     {
128         if (docInfo == null)
129             getDocumentInfo();
130         return docInfo.getEstimatedDocumentLength();
131     }
132     
133     public long getNodeCount() throws XMLDBCException
134     {
135         if (docInfo == null)
136             getDocumentInfo();
137         return docInfo.nodeCount;
138      }
139     
140     ///////////////////////////////////////////////////////////////////////////
141
// PRIVATE
142
///////////////////////////////////////////////////////////////////////////
143
private void getDocumentInfo() throws RepositoryException
144     {
145         docInfo = ((_RepositoryCollection)getCollection()).getMetadata().getDocumentInfo(getIdentifier());
146     }
147 }
148
Popular Tags