001/* 002 * ModeShape (http://www.modeshape.org) 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 */ 016package org.modeshape.schematic.internal; 017 018import org.modeshape.schematic.DocumentLibrary; 019import org.modeshape.schematic.SchemaLibrary; 020import org.modeshape.schematic.document.Document; 021import org.modeshape.schematic.internal.document.Paths; 022import org.modeshape.schematic.internal.schema.DocumentTransformer; 023import org.modeshape.schematic.internal.schema.SchemaDocument; 024import org.modeshape.schematic.internal.schema.SchemaDocumentCache; 025import org.modeshape.schematic.internal.schema.ValidationResult; 026 027public class InMemorySchemaLibrary implements SchemaLibrary { 028 029 private final DocumentLibrary documents; 030 private final SchemaDocumentCache schemaDocuments; 031 032 public InMemorySchemaLibrary( String name ) { 033 this.documents = new InMemoryDocumentLibrary(name); 034 this.schemaDocuments = new SchemaDocumentCache(this, null); 035 } 036 037 @Override 038 public String getName() { 039 return documents.getName(); 040 } 041 042 @Override 043 public Document get( String key ) { 044 return documents.get(key); 045 } 046 047 @Override 048 public Document put( String key, 049 Document document ) { 050 Document result = documents.put(key, document); 051 schemaDocuments.remove(key); 052 return result; 053 } 054 055 @Override 056 public Document putIfAbsent( String key, 057 Document document ) { 058 Document result = documents.putIfAbsent(key, document); 059 schemaDocuments.remove(key); 060 return result; 061 } 062 063 @Override 064 public Document replace( String key, 065 Document document ) { 066 Document result = documents.replace(key, document); 067 schemaDocuments.remove(key); 068 return result; 069 } 070 071 @Override 072 public Document remove( String key ) { 073 Document result = documents.remove(key); 074 schemaDocuments.remove(key); 075 return result; 076 } 077 078 @Override 079 public Results validate( Document document, 080 String schemaUri ) { 081 ValidationResult result = new ValidationResult(); 082 SchemaDocument schema = schemaDocuments.get(schemaUri, result); 083 if (schema != null) { 084 schema.getValidator().validate(null, null, document, Paths.rootPath(), result, schemaDocuments); 085 } 086 return result; 087 } 088 089 @Override 090 public Document convertValues( Document document, 091 Results results ) { 092 return DocumentTransformer.convertValuesWithMismatchedTypes(document, results); 093 } 094 095 @Override 096 public Document convertValues( Document document, 097 String schemaUri ) { 098 Results results = validate(document, schemaUri); 099 return convertValues(document, results); 100 } 101}