Skip to content

Commit

Permalink
Closes #13
Browse files Browse the repository at this point in the history
  • Loading branch information
jpav committed Oct 26, 2013
1 parent f8648bd commit 58d83da
Show file tree
Hide file tree
Showing 9 changed files with 311 additions and 52 deletions.
5 changes: 5 additions & 0 deletions modeshape-modeler-xsd/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
<type>test-jar</type>
</dependency>

<dependency>
<groupId>org.modeshape</groupId>
<artifactId>modeshape-sequencer-xsd</artifactId>
</dependency>

<!-- Test -->
<dependency>
<groupId>junit</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/*
* Polyglotter (http://polyglotter.org)
* See the COPYRIGHT.txt file distributed with this work for information
* regarding copyright ownership. Some portions may be licensed
* to Red Hat, Inc. under one or more contributor license agreements.
* See the AUTHORS.txt file in the distribution for a full listing of
* individual contributors.
*
* Polyglotter is free software. Unless otherwise indicated, all code in Polyglotter
* is licensed to you under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* Polyglotter is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.modeshape.modeler.xsd;

import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;

import org.modeshape.modeler.Model;
import org.modeshape.modeler.ModelerException;
import org.modeshape.modeler.extensions.Desequencer;
import org.modeshape.modeler.internal.ModelImpl;
import org.modeshape.modeler.internal.Task;
import org.modeshape.sequencer.sramp.SrampLexicon;
import org.modeshape.sequencer.xsd.XsdLexicon;

/**
*
*/
public class XsdDesequencer implements Desequencer {

PrintWriter writer;
String xsdPrefix;
final Map< String, String > namespacePrefixByUri = new HashMap<>();
final Map< String, Node > complexTypeByName = new HashMap<>();
String targetNamespace;

/**
* {@inheritDoc}
*
* @see org.modeshape.modeler.extensions.Desequencer#execute(org.modeshape.modeler.Model, java.io.OutputStream)
*/
@Override
public void execute( final Model model,
final OutputStream stream ) throws ModelerException {
( ( ModelImpl ) model ).manager.run( new Task< Void >() {

@Override
public Void run( final Session session ) throws Exception {
final Node node = session.getNode( model.absolutePath() );
// new JcrTools().printSubgraph( node );
writer = new PrintWriter( stream, true );
// Print XML declaration
writer.print( "<?xml version=\"1.0\" encoding=\"" );
writer.print( node.getProperty( SrampLexicon.CONTENT_ENCODING ).getString() );
writer.println( "\"?>" );
// Find XSD namespace declaration & build namespace-prefix-by-URI map
for ( final PropertyIterator iter = node.getProperties( "xmlns*" ); iter.hasNext(); ) {
final Property prop = iter.nextProperty();
final String uri = prop.getString();
final int ndx = prop.getName().indexOf( ':' ) + 1;
String prefix = prop.getName().substring( ndx );
if ( ndx > 0 ) prefix += ':';
namespacePrefixByUri.put( uri, prefix );
if ( uri.equals( XsdLexicon.Namespace.URI ) ) xsdPrefix = prefix;
}
// Print schema
print( 0, node );
return null;
}
} );
}

void print( final int indentLevel,
final Node node ) throws RepositoryException {
String element = null;
final String type = node.getPrimaryNodeType().getName();
if ( type.equals( XsdLexicon.ATTRIBUTE_DECLARATION ) ) element = "attribute";
else if ( type.equals( XsdLexicon.CHOICE ) ) element = "choice";
else if ( type.equals( XsdLexicon.COMPLEX_TYPE_DEFINITION ) ) element = "complexType";
else if ( type.equals( XsdLexicon.ELEMENT_DECLARATION ) ) element = "element";
else if ( type.equals( XsdLexicon.IMPORT ) ) element = "import";
else if ( type.equals( XsdLexicon.SCHEMA_DOCUMENT ) ) element = "schema";
else if ( type.equals( XsdLexicon.SEQUENCE ) ) element = "sequence";
else throw new UnsupportedOperationException( node.toString() );
printIndent( indentLevel );
writer.print( '<' + xsdPrefix + element );
printAttributes( indentLevel, node );
if ( node.hasNodes() || node.hasProperty( SrampLexicon.DESCRIPTION ) ) {
writer.println( '>' );
int childIndentLevel = indentLevel + 1;
printAnnotation( childIndentLevel, node );
if ( element.equals( "complexType" ) ) {
complexTypeByName.put( node.getProperty( XsdLexicon.NC_NAME ).getString(), node );
final String baseType = node.getProperty( XsdLexicon.BASE_TYPE_NAME ).getString();
final String method = node.getProperty( XsdLexicon.METHOD ).getString();
final String baseTypeNamespace = node.getProperty( XsdLexicon.BASE_TYPE_NAMESPACE ).getString();
if ( !baseType.equals( "anyType" )
|| !method.equals( "restriction" )
|| !baseTypeNamespace.equals( XsdLexicon.Namespace.URI ) ) {
final String contentType = complexTypeByName.get( baseType ) == null ? "simpleContent" : "complexContent";
printIndent( indentLevel + 1 );
writer.println( '<' + xsdPrefix + contentType + '>' );
printIndent( indentLevel + 2 );
writer.println( '<' + xsdPrefix + method + " base=\"" + namespacePrefixByUri.get( baseTypeNamespace )
+ baseType + "\">" );
childIndentLevel += 2;
for ( final NodeIterator iter = node.getNodes(); iter.hasNext(); )
print( childIndentLevel, iter.nextNode() );
printIndent( indentLevel + 2 );
writer.println( "</" + xsdPrefix + method + '>' );
printIndent( indentLevel + 1 );
writer.println( "</" + xsdPrefix + contentType + '>' );
} else for ( final NodeIterator iter = node.getNodes(); iter.hasNext(); )
print( childIndentLevel, iter.nextNode() );
} else for ( final NodeIterator iter = node.getNodes(); iter.hasNext(); )
print( childIndentLevel, iter.nextNode() );
printIndent( indentLevel );
writer.println( "</" + xsdPrefix + element + '>' );
} else writer.println( "/>" );
}

private void printAnnotation( final int indentLevel,
final Node node ) throws RepositoryException {
if ( !node.hasProperty( SrampLexicon.DESCRIPTION ) ) return;
final Property prop = node.getProperty( SrampLexicon.DESCRIPTION );
printIndent( indentLevel );
writer.println( '<' + xsdPrefix + XsdLexicon.ANNOTATION + '>' );
printIndent( indentLevel + 1 );
writer.println( '<' + xsdPrefix + "documentation>" );
printIndent( indentLevel + 2 );
writer.println( prop.getString() );
printIndent( indentLevel + 1 );
writer.println( "</" + xsdPrefix + "documentation>" );
printIndent( indentLevel );
writer.println( "</" + xsdPrefix + XsdLexicon.ANNOTATION + '>' );
}

private void printAttribute( final String name,
final String value ) {
writer.print( ' ' + name + "=\"" + value + "\"" );
}

private void printAttributes( final int indentLevel,
final Node node ) throws RepositoryException {
String maxOccurs = null;
for ( final PropertyIterator iter = node.getProperties(); iter.hasNext(); ) {
final Property prop = iter.nextProperty();
final String name = prop.getName();
if ( name.equals( XsdLexicon.NC_NAME ) ) printAttribute( "name", prop.getString() );
else if ( name.equals( XsdLexicon.TYPE_NAME ) ) {
printAttribute( "type",
namespacePrefixByUri.get( node.getProperty( XsdLexicon.TYPE_NAMESPACE ).getString() )
+ prop.getString() );
} else if ( name.equals( XsdLexicon.MIN_OCCURS ) ) {
if ( prop.getLong() != 1 ) printAttribute( "minOccurs", prop.getString() );
maxOccurs = "unbounded";
} else if ( name.equals( XsdLexicon.MAX_OCCURS ) ) {
if ( prop.getLong() != 1 ) printAttribute( "maxOccurs", prop.getString() );
maxOccurs = null;
} else if ( name.equals( XsdLexicon.USE ) ) {
if ( !prop.getString().equals( "optional" ) ) printAttribute( "use", prop.getString() );
} else if ( name.startsWith( "xmlns" ) ) printAttribute( name, prop.getString() );
else if ( name.equals( XsdLexicon.NAMESPACE ) ) {
if ( !prop.getString().equals( targetNamespace ) ) printAttribute( "namespace", prop.getString() );
}
else if ( name.equals( XsdLexicon.SCHEMA_LOCATION ) ) printAttribute( "schemaLocation", prop.getString() );
else if ( name.equals( "targetNamespace" ) ) {
targetNamespace = prop.getString();
printAttribute( name, targetNamespace );
}
}
if ( maxOccurs != null ) printAttribute( "maxOccurs", maxOccurs );
}

private void printIndent( final int indentLevel ) {
for ( int ndx = indentLevel; --ndx >= 0; )
writer.print( " " );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,50 +27,9 @@
* The model lexicon for the XSD modeler.
*/
public interface XsdLexicon {

/**
* The name of the XSD model node. Value is {@value} .
*/
String MODEL_TYPE_ID = "org.modeshape.modeler.xsd.Xsd";

/**
* The XSD namespace prefix. Value is {@value} .
*/
String NAMESPACE_PREFIX = "xs";

/**
* The XSD namespace prefix appended with the local name separator. Value is {@value} .
*/
String NAMESPACE_PREFIX_WITH_SEPARATOR = NAMESPACE_PREFIX + ':';

/**
* The name of the XSD import element. Value is {@value} .
*/
String IMPORT = NAMESPACE_PREFIX_WITH_SEPARATOR + "import";

/**
* The name of the XSD include element. Value is {@value} .
*/
String INCLUDE = NAMESPACE_PREFIX_WITH_SEPARATOR + "include";

/**
* The name of the XSD schema document node type. Value is {@value} .
*/
String SCHEMA_DOCUMENT = NAMESPACE_PREFIX_WITH_SEPARATOR + "schemaDocument";

/**
* The name of the XSD schema location attribute. Value is {@value} .
*/
String SCHEMA_LOCATION = NAMESPACE_PREFIX_WITH_SEPARATOR + "schemaLocation";

/**
* The name of the XSD namespace attribute. Value is {@value} .
*/
String NAMESPACE = NAMESPACE_PREFIX_WITH_SEPARATOR + "namespace";

/**
* The name of the XSD redefine element. Value is {@value} .
*/
String REDEFINE = NAMESPACE_PREFIX_WITH_SEPARATOR + "redefine";

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@
import org.modeshape.modeler.ModelerException;
import org.modeshape.modeler.extensions.DependencyProcessor;
import org.modeshape.modeler.internal.ModelerLexicon;
import org.modeshape.modeler.xsd.XsdLexicon;
import org.modeshape.modeler.xsd.XsdModelerI18n;
import org.modeshape.sequencer.xsd.XsdLexicon;
import org.polyglotter.common.Logger;

/**
* The XSD dependency processor for the ModeShape modeler.
*/
public final class XsdDependencyProcessor implements DependencyProcessor, XsdLexicon {
public final class XsdDependencyProcessor implements DependencyProcessor {

private static final Logger LOGGER = Logger.getLogger( XsdDependencyProcessor.class );

Expand Down Expand Up @@ -86,7 +86,9 @@ private boolean dependencyNode( final Node node ) throws Exception {
assert ( node != null );

final String primaryType = node.getPrimaryNodeType().getName();
return ( IMPORT.equals( primaryType ) || INCLUDE.equals( primaryType ) || REDEFINE.equals( primaryType ) );
return ( XsdLexicon.IMPORT.equals( primaryType )
|| XsdLexicon.INCLUDE.equals( primaryType )
|| XsdLexicon.REDEFINE.equals( primaryType ) );
}

/**
Expand Down Expand Up @@ -114,7 +116,7 @@ public String process( final String artifactPath,
while ( itr.hasNext() ) {
final Node kid = itr.nextNode();

if ( SCHEMA_DOCUMENT.equals( kid.getPrimaryNodeType().getName() ) ) {
if ( XsdLexicon.SCHEMA_DOCUMENT.equals( kid.getPrimaryNodeType().getName() ) ) {
schemaNode = kid;
break;
}
Expand Down Expand Up @@ -157,7 +159,7 @@ public String process( final String artifactPath,
dependenciesNode.addNode( ModelerLexicon.DEPENDENCY, ModelerLexicon.DEPENDENCY );

// set input property
final Property locationProp = kid.getProperty( SCHEMA_LOCATION );
final Property locationProp = kid.getProperty( XsdLexicon.SCHEMA_LOCATION );
final String location = locationProp.getString();
dependencyNode.setProperty( ModelerLexicon.SOURCE_REFERENCE_PROPERTY, new String[] { location } );
LOGGER.debug( "Setting dependency source reference property to '%s'", location );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Polyglotter (http://polyglotter.org)
* See the COPYRIGHT.txt file distributed with this work for information
* regarding copyright ownership. Some portions may be licensed
* to Red Hat, Inc. under one or more contributor license agreements.
* See the AUTHORS.txt file in the distribution for a full listing of
* individual contributors.
*
* Polyglotter is free software. Unless otherwise indicated, all code in Polyglotter
* is licensed to you under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* Polyglotter is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.modeshape.modeler.xsd;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

import java.io.ByteArrayOutputStream;
import java.io.File;

import org.junit.Test;
import org.modeshape.modeler.Model;
import org.modeshape.modeler.xsd.test.XsdBaseTest;

@SuppressWarnings( "javadoc" )
public class XsdDesquencerTest extends XsdBaseTest {

@Test
public void shouldDesequence() throws Exception {
modelTypeManager().install( SRAMP_MODEL_TYPE_CATEGORY );
modelTypeManager().install( XSD_MODEL_TYPE_CATEGORY );
final Model model = modeler().generateModel( new File( "src/test/resources/Books/Books.xsd" ),
null,
modelTypeManager().modelType( XSD_MODEL_TYPE_ID ) );
try ( final ByteArrayOutputStream stream = new ByteArrayOutputStream() ) {
new XsdDesequencer().execute( model, stream );
assertThat( stream.toString().startsWith( XML_DECLARATION + "\n<xsd:schema " ), is( true ) );
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Polyglotter (http://polyglotter.org)
* See the COPYRIGHT.txt file distributed with this work for information
* regarding copyright ownership. Some portions may be licensed
* to Red Hat, Inc. under one or more contributor license agreements.
* See the AUTHORS.txt file in the distribution for a full listing of
* individual contributors.
*
* Polyglotter is free software. Unless otherwise indicated, all code in Polyglotter
* is licensed to you under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* Polyglotter is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.modeshape.modeler.xsd.test;

import org.modeshape.modeler.test.BaseTest;

@SuppressWarnings( "javadoc" )
public abstract class XsdBaseTest extends BaseTest {

protected static final String SRAMP_MODEL_TYPE_CATEGORY = "sramp";
protected static final String XSD_MODEL_TYPE_CATEGORY = "xsd";
protected static final String XSD_MODEL_TYPE_ID = "org.modeshape.modeler.xsd.Xsd";
}
Loading

0 comments on commit 58d83da

Please sign in to comment.