1 20 21 package org.apache.directory.ldapstudio.schemas.io; 22 23 24 import java.io.FileNotFoundException ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.net.URL ; 28 import java.text.ParseException ; 29 30 import org.apache.directory.ldapstudio.schemas.Messages; 31 import org.apache.directory.server.core.tools.schema.AttributeTypeLiteral; 32 import org.apache.directory.server.core.tools.schema.ObjectClassLiteral; 33 import org.apache.directory.server.core.tools.schema.OpenLdapSchemaParser; 34 import org.apache.log4j.Logger; 35 import org.eclipse.swt.SWT; 36 import org.eclipse.swt.widgets.MessageBox; 37 import org.eclipse.ui.PlatformUI; 38 39 40 45 public class SchemaParser 46 { 47 48 private static Logger logger = Logger.getLogger( SchemaParser.class ); 49 52 53 private URL fileURL = null; 54 private AttributeTypeLiteral[] attributeTypes = null; 55 private ObjectClassLiteral[] objectClasses = null; 56 57 58 61 62 66 public AttributeTypeLiteral[] getAttributeTypes() 67 { 68 return attributeTypes; 69 } 70 71 72 76 public ObjectClassLiteral[] getObjectClasses() 77 { 78 return objectClasses; 79 } 80 81 82 private void setAttributeTypes( AttributeTypeLiteral[] attributeTypes ) 83 { 84 this.attributeTypes = attributeTypes; 85 } 86 87 88 private void setObjectClasses( ObjectClassLiteral[] objectClasses ) 89 { 90 this.objectClasses = objectClasses; 91 } 92 93 94 97 98 private SchemaParser( URL url ) 99 { 100 this.fileURL = url; 101 } 102 103 104 109 public static SchemaParser parserFromURL( URL url ) 110 { 111 return new SchemaParser( url ); 112 113 } 114 115 116 119 120 125 public void parse() throws IOException , ParseException 126 { 127 InputStream in = null; 128 in = fileURL.openStream(); 129 130 if ( in == null ) 131 throw new FileNotFoundException ( Messages.getString( "SchemaParser.No_path_or_url_specified" ) ); 133 OpenLdapSchemaParser parser = new OpenLdapSchemaParser(); 134 try 135 { 136 parser.parse( in ); 137 } 138 catch ( ParseException e ) 139 { 140 logger.error( "An error occured when parsing the file - " + e.getMessage() ); MessageBox messageBox = new MessageBox( PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), 142 SWT.OK | SWT.ICON_ERROR ); 143 messageBox 144 .setMessage( Messages.getString( "SchemaParser.An_error_has_occurred_when_parsing_the_file" ) + fileURL.toString() + Messages.getString( "SchemaParser.The_schema_cannot_be_opened" ) + " " + Messages.getString( "SchemaParser.See_log_file_for_debug_information_about_the_schema." ) ); messageBox.open(); 146 throw e; 147 } 148 149 generateAttributeTypes( parser ); 150 generateObjectClasses( parser ); 151 } 152 153 154 158 @SuppressWarnings ("unchecked") private void generateAttributeTypes( OpenLdapSchemaParser parser ) 160 { 161 int size = parser.getAttributeTypes().size(); 162 setAttributeTypes( new AttributeTypeLiteral[size] ); 163 setAttributeTypes( ( AttributeTypeLiteral[] ) parser.getAttributeTypes().toArray( getAttributeTypes() ) ); 164 } 165 166 167 171 @SuppressWarnings ("unchecked") private void generateObjectClasses( OpenLdapSchemaParser parser ) 173 { 174 int size = parser.getObjectClassTypes().size(); 175 setObjectClasses( new ObjectClassLiteral[size] ); 176 setObjectClasses( ( ObjectClassLiteral[] ) parser.getObjectClassTypes().toArray( getObjectClasses() ) ); 177 } 178 } 179 | Popular Tags |