protected void registerExtensionSchemas(SchemaRegistryImpl schemaRegistry) throws SchemaException, FileNotFoundException { if (extraSchemaDir != null && extraSchemaDir.exists()) { schemaRegistry.registerPrismSchemasFromDirectory(extraSchemaDir); } }
public void registerPrismSchemasFromDirectory(File directory) throws FileNotFoundException, SchemaException { File[] fileArray = directory.listFiles(); if (fileArray != null) { List<File> files = Arrays.asList(fileArray); // Sort the filenames so we have deterministic order of loading // This is useful in tests but may come handy also during customization Collections.sort(files); for (File file: files) { if (file.getName().startsWith(".")) { // skip dotfiles. this will skip SVN data and similar things continue; } if (file.isDirectory()) { registerPrismSchemasFromDirectory(file); } if (file.isFile()) { registerPrismSchemaFile(file); } } } }
schemaRegistry.registerPrismSchemasFromDirectory(file); } catch (Exception ex) { throw new SchemaException(ex.getMessage(), ex);
/** * Test if a schema directory can be loaded to the schema registry. This contains definition of * user extension, therefore check if it is applied to the user definition. */ @Test public void testUserExtensionSchemaLoad() throws SAXException, IOException, SchemaException { System.out.println("===[ testUserExtensionSchemaLoad ]==="); PrismContext context = constructPrismContext(); SchemaRegistryImpl reg = (SchemaRegistryImpl) context.getSchemaRegistry(); reg.registerPrismSchemasFromDirectory(EXTRA_SCHEMA_DIR); context.initialize(); System.out.println("Initialized registry"); System.out.println(reg.debugDump()); // Try midpoint schemas by parsing a XML file PrismSchema schema = reg.getPrismSchema(NS_FOO); System.out.println("Parsed foo schema:"); System.out.println(schema.debugDump()); // TODO: assert user schema = reg.getPrismSchema(NS_USER_EXT); System.out.println("Parsed user ext schema:"); System.out.println(schema.debugDump()); ComplexTypeDefinition userExtComplexType = schema.findComplexTypeDefinition(USER_EXTENSION_TYPE_QNAME); assertEquals("Extension type ref does not match", USER_TYPE_QNAME, userExtComplexType.getExtensionForType()); }
@Test public void testUserExtensionSchemaParseUser() throws SAXException, IOException, SchemaException { System.out.println("===[ testUserExtensionSchemaParseUser ]==="); Document dataDoc = DOMUtil.parseFile(USER_JACK_FILE_XML); PrismContext context = constructPrismContext(); SchemaRegistryImpl reg = (SchemaRegistryImpl) context.getSchemaRegistry(); reg.registerPrismSchemasFromDirectory(EXTRA_SCHEMA_DIR); context.initialize(); // Parsing user PrismObject<UserType> user = context.parserFor(DOMUtil.getFirstChildElement(dataDoc)).compat().parse(); // items from user extension are not correctly defined in the schema assertNotNull("No definition for user", user.getDefinition()); System.out.println("Parsed root object:"); System.out.println(user.debugDump()); // TODO: assert user // Try javax schemas by validating a XML file Schema javaxSchema = reg.getJavaxSchema(); assertNotNull(javaxSchema); Validator validator = javaxSchema.newValidator(); DOMResult validationResult = new DOMResult(); validator.validate(new DOMSource(dataDoc),validationResult); // System.out.println("Validation result:"); // System.out.println(DOMUtil.serializeDOMToString(validationResult.getNode())); }
/** * Test if a schema directory can be loaded to the schema registry. This contains definition of * user extension, therefore check if it is applied to the user definition. */ @Test public void testTypeOverride() throws SAXException, IOException, SchemaException { System.out.println("===[ testTypeOverride ]==="); PrismContext context = constructPrismContext(); SchemaRegistryImpl reg = (SchemaRegistryImpl) context.getSchemaRegistry(); reg.registerPrismSchemasFromDirectory(EXTRA_SCHEMA_DIR); context.initialize(); PrismSchema schema = reg.getPrismSchema(NS_ROOT); System.out.println("Parsed root schema:"); System.out.println(schema.debugDump()); PrismContainerDefinition rootContDef = schema.findContainerDefinitionByElementName(new QName(NS_ROOT,"root")); assertNotNull("Not <root> definition", rootContDef); PrismContainerDefinition extensionContDef = rootContDef.findContainerDefinition(new ItemName(NS_FOO, "extension")); assertNotNull("Not <extension> definition", extensionContDef); assertEquals("Wrong <extension> type", new QName(NS_ROOT, "MyExtensionType"), extensionContDef.getTypeName()); }
@Test public void testUserExtensionSchemaSchemaRegistry() throws SAXException, IOException, SchemaException { System.out.println("===[ testUserExtensionSchemaAsObjectSchema ]==="); PrismContext context = constructPrismContext(); SchemaRegistryImpl reg = (SchemaRegistryImpl) context.getSchemaRegistry(); reg.registerPrismSchemasFromDirectory(EXTRA_SCHEMA_DIR); context.initialize(); PrismObjectDefinition<UserType> userDef = reg.findObjectDefinitionByType(USER_TYPE_QNAME); System.out.println("User definition:"); System.out.println(userDef.debugDump()); assertUserDefinition(userDef); PrismObjectDefinition<UserType> usedDefByClass = reg.findObjectDefinitionByCompileTimeClass(UserType.class); assertUserDefinition(usedDefByClass); PrismObjectDefinition<UserType> userDefByElement = reg.findObjectDefinitionByElementName(USER_QNAME); assertUserDefinition(userDefByElement); }
public static PrismContextImpl constructPrismContext(File extraSchema) throws SchemaException, FileNotFoundException { SchemaRegistryImpl schemaRegistry = new SchemaRegistryImpl(); schemaRegistry.setCatalogResourceName(TEST_CATALOG_RESOURCE_NAME); DynamicNamespacePrefixMapper prefixMapper = new GlobalDynamicNamespacePrefixMapper(); // Set default namespace? schemaRegistry.setNamespacePrefixMapper(prefixMapper); schemaRegistry.registerPrismDefaultSchemaResource("xml/ns/test/foo-1.xsd", "foo", ObjectFactory.class.getPackage()); schemaRegistry.registerPrismSchemaResource("xml/ns/test/foo-types-1.xsd", "foot", null); schemaRegistry.registerPrismSchemaResource("xml/ns/public/types-3.xsd", "t", com.evolveum.prism.xml.ns._public.types_3.ObjectFactory.class.getPackage()); schemaRegistry.registerPrismSchemaResource("xml/ns/public/query-3.xsd", "q", com.evolveum.prism.xml.ns._public.query_3.ObjectFactory.class.getPackage()); schemaRegistry.registerPrismSchemasFromDirectory(SCHEMA_DIR); if (extraSchema != null){ schemaRegistry.registerPrismSchemaFile(extraSchema); } prefixMapper.registerPrefix(XMLConstants.W3C_XML_SCHEMA_NS_URI, DOMUtil.NS_W3C_XML_SCHEMA_PREFIX, false); prefixMapper.registerPrefix(PrismConstants.NS_ANNOTATION, PrismConstants.PREFIX_NS_ANNOTATION, false); prefixMapper.registerPrefix(PrismInternalTestUtil.NS_WEAPONS, PrismInternalTestUtil.NS_WEAPONS_PREFIX, false); PrismContextImpl prismContext = PrismContextImpl.create(schemaRegistry); prismContext.setObjectsElementName(new QName("http://midpoint.evolveum.com/xml/ns/public/common/common-3", "objects")); return prismContext; }