KickJava   Java API By Example, From Geeks To Geeks.

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


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  * SchemaSAXHandler.java
25  *
26  * Created on 31 mai 2001, 16:55
27  */

28
29 package org.xquark.mapper.storage;
30
31 import java.util.List JavaDoc;
32
33 import org.xml.sax.InputSource JavaDoc;
34 import org.xquark.mapper.RepositoryException;
35 import org.xquark.mapper.mapping.MappingConstants;
36 import org.xquark.mapper.util.DestructionToken;
37 import org.xquark.schema.Schema;
38 import org.xquark.schema.SchemaConstants;
39 import org.xquark.schema.SchemaManager;
40 import org.xquark.schema.validation.SchemaValidationContext;
41 import org.xquark.xml.xdbc.XMLDBCException;
42
43 /**
44  *
45  */

46 public class SchemaSAXHandler extends CollectionFiler
47 {
48 private static final String JavaDoc RCSRevision = "$Revision: 1.2 $";
49 private static final String JavaDoc RCSName = "$Name: $";
50     
51     /** Creates new SchemaSAXHandler */
52     public SchemaSAXHandler(_RepositoryCollection collection,
53                             SchemaValidationContext schemaContext,
54                             DestructionToken destructor)
55     throws XMLDBCException
56     {
57         super(collection, schemaContext, true, destructor); // TO IMPROVE in the future prefix could not be stored if schema of schemas is available
58
}
59     
60     protected void endParsing()throws Exception JavaDoc
61     {
62         super.endParsing();
63         flushBuffer(); // to be sure the schema is stored before loading
64

65         // Load the schema to validate it
66
SchemaManager manager = collection.getMetadata().getRepository().getSchemaManager();
67         
68         // load schema from input source cos' file ID ain't namespace
69
Schema schema = manager.loadSchema(collection.getRepositoryConnection(), new InputSource JavaDoc(getDocumentId()));
70         if (schema == null)
71             throw new RepositoryException(RepositoryException.CONSISTENCY_ERROR, "The schema could not be loaded.");
72         
73         String JavaDoc targetNamespace = schema.getNamespace();
74         if (targetNamespace == null)
75             targetNamespace = NO_NAMESPACE_SCHEMA_ID;
76         else if (targetNamespace.equals(SchemaConstants.XMLSCHEMA_URI)
77                     || targetNamespace.equals(MappingConstants.MAPPING_URI))
78             throw new RepositoryException(RepositoryException.ALREADY_EXISTS,
79             "This schema could not be stored in the repository since it is a system schema.");
80
81         // first check it's not in use
82
List JavaDoc usingCollections = collection.getRepositoryConnection().getCollectionsUsingNamespace(targetNamespace);
83         if (usingCollections.size() != 0)
84             throw new RepositoryException(RepositoryException.NOT_ALLOWED,
85             "This namespace is still used by documents stored in the following collection: "
86             + usingCollections + ". Adding such a schema would alter the metadata for these documents.");
87         else
88         {
89             List JavaDoc usingMappings = collection.getRepositoryConnection().getMappingsUsingNamespace(targetNamespace);
90             if (usingMappings.size() != 0)
91                 throw new RepositoryException(RepositoryException.NOT_ALLOWED,
92                 "This namespace is still used by mapping files: "
93                 + usingMappings + ". Adding such a schema would alter the metadata for these documents.");
94         }
95         
96         // Check the schema does not already exists in the system repository
97
if (collection.containsDocument(targetNamespace)) // if not already opened or present in the system repository
98
throw new RepositoryException(RepositoryException.ALREADY_EXISTS,
99             "The namespace of the schema is already in use in the system repository.");
100         // rename the schema document
101
((SchemaXMLCollection)collection).renameDocument(getDocumentId(), targetNamespace, false); // this throws an exception
102
// change it so the getDocument is consistent
103
docID = targetNamespace;
104     }
105     
106 }
107
Popular Tags