| 1 |
package com.thaiopensource.validate.rng.impl; |
| 2 |
|
| 3 |
import com.thaiopensource.validate.IncorrectSchemaException; |
| 4 |
import com.thaiopensource.validate.auto.SchemaReceiver; |
| 5 |
import com.thaiopensource.validate.Schema; |
| 6 |
import com.thaiopensource.validate.ValidateProperty; |
| 7 |
import com.thaiopensource.validate.prop.rng.RngProperty; |
| 8 |
import com.thaiopensource.validate.prop.wrap.WrapProperty; |
| 9 |
import com.thaiopensource.validate.auto.SchemaFuture; |
| 10 |
import com.thaiopensource.relaxng.parse.ParseReceiver; |
| 11 |
import com.thaiopensource.relaxng.parse.BuildException; |
| 12 |
import com.thaiopensource.relaxng.parse.IllegalSchemaException; |
| 13 |
import com.thaiopensource.relaxng.pattern.SchemaPatternBuilder; |
| 14 |
import com.thaiopensource.relaxng.pattern.PatternFuture; |
| 15 |
import com.thaiopensource.relaxng.pattern.SchemaBuilderImpl; |
| 16 |
import com.thaiopensource.util.PropertyMap; |
| 17 |
import com.thaiopensource.datatype.DatatypeLibraryLoader; |
| 18 |
import org.relaxng.datatype.DatatypeLibraryFactory; |
| 19 |
import org.xml.sax.ErrorHandler; |
| 20 |
import org.xml.sax.SAXException; |
| 21 |
import org.xml.sax.XMLReader; |
| 22 |
|
| 23 |
import java.io.IOException; |
| 24 |
|
| 25 |
public class SchemaReceiverImpl implements SchemaReceiver { |
| 26 |
private final ParseReceiver parser; |
| 27 |
private final PropertyMap properties; |
| 28 |
|
| 29 |
public SchemaReceiverImpl(ParseReceiver parser, PropertyMap properties) { |
| 30 |
this.parser = parser; |
| 31 |
this.properties = properties; |
| 32 |
} |
| 33 |
|
| 34 |
public SchemaFuture installHandlers(XMLReader xr) throws SAXException { |
| 35 |
final SchemaPatternBuilder pb = new SchemaPatternBuilder(); |
| 36 |
ErrorHandler eh = properties.get(ValidateProperty.ERROR_HANDLER); |
| 37 |
DatatypeLibraryFactory dlf = properties.get(RngProperty.DATATYPE_LIBRARY_FACTORY); |
| 38 |
if (dlf == null) |
| 39 |
dlf = new DatatypeLibraryLoader(); |
| 40 |
final PatternFuture pf = SchemaBuilderImpl.installHandlers(parser, xr, eh, dlf, pb); |
| 41 |
return new SchemaFuture() { |
| 42 |
public Schema getSchema() throws IncorrectSchemaException, SAXException, IOException { |
| 43 |
try { |
| 44 |
return SchemaReaderImpl.wrapPattern(pf.getPattern(properties.contains(WrapProperty.ATTRIBUTE_OWNER)), |
| 45 |
pb, properties); |
| 46 |
} |
| 47 |
catch (IllegalSchemaException e) { |
| 48 |
throw new IncorrectSchemaException(); |
| 49 |
} |
| 50 |
} |
| 51 |
public RuntimeException unwrapException(RuntimeException e) throws SAXException, IOException, IncorrectSchemaException { |
| 52 |
if (e instanceof BuildException) { |
| 53 |
try { |
| 54 |
return SchemaBuilderImpl.unwrapBuildException((BuildException)e); |
| 55 |
} |
| 56 |
catch (IllegalSchemaException ise) { |
| 57 |
throw new IncorrectSchemaException(); |
| 58 |
} |
| 59 |
} |
| 60 |
return e; |
| 61 |
} |
| 62 |
}; |
| 63 |
} |
| 64 |
} |