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 */ 016 017package org.modeshape.sequencer.xsd; 018 019import static org.modeshape.sequencer.xsd.XsdLexicon.SCHEMA_DOCUMENT; 020import java.io.IOException; 021import java.io.InputStream; 022import javax.jcr.Binary; 023import javax.jcr.NamespaceRegistry; 024import javax.jcr.Node; 025import javax.jcr.Property; 026import javax.jcr.RepositoryException; 027import org.modeshape.common.util.CheckArg; 028import org.modeshape.jcr.api.nodetype.NodeTypeManager; 029import org.modeshape.sequencer.sramp.AbstractSrampSequencer; 030 031/** 032 * A sequencer that processes and extract the schema object model from XML Schema Document files. 033 */ 034public class XsdSequencer extends AbstractSrampSequencer { 035 036 public static final class MimeTypeConstants { 037 public static final String APPLICATION_XML = "application/xml"; 038 public static final String TEXT_XML = "text/xml"; 039 040 /** This is not a valid MIME type, but we're using it in case other people use it */ 041 @Deprecated 042 public static final String XSD = "application/xsd"; 043 @Deprecated 044 public static final String XSD_XML = "application/xsd+xml"; 045 } 046 047 @Override 048 public void initialize( NamespaceRegistry registry, 049 NodeTypeManager nodeTypeManager ) throws RepositoryException, IOException { 050 super.initialize(registry, nodeTypeManager); 051 registerNodeTypes("xsd.cnd", nodeTypeManager, true); 052 registerDefaultMimeTypes(MimeTypeConstants.XSD, // just in case clients set these mime type 053 MimeTypeConstants.XSD_XML, // on nodes to be sequenced 054 MimeTypeConstants.APPLICATION_XML, 055 MimeTypeConstants.TEXT_XML); 056 } 057 058 @Override 059 public boolean execute( Property inputProperty, 060 Node outputNode, 061 Context context ) throws Exception { 062 Binary binaryValue = inputProperty.getBinary(); 063 CheckArg.isNotNull(binaryValue, "binary"); 064 065 if (outputNode.isNew()) { 066 outputNode.setPrimaryType(SCHEMA_DOCUMENT); 067 } else { 068 outputNode = outputNode.addNode(SCHEMA_DOCUMENT, SCHEMA_DOCUMENT); 069 } 070 try (InputStream stream = binaryValue.getStream()) { 071 new XsdReader(context).read(stream, outputNode); 072 } 073 return true; 074 } 075}