本文整理汇总了Java中javax.xml.bind.ValidationEventHandler类的典型用法代码示例。如果您正苦于以下问题:Java ValidationEventHandler类的具体用法?Java ValidationEventHandler怎么用?Java ValidationEventHandler使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ValidationEventHandler类属于javax.xml.bind包,在下文中一共展示了ValidationEventHandler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setJaxbContext
import javax.xml.bind.ValidationEventHandler; //导入依赖的package包/类
public synchronized void setJaxbContext(Class... classesToBeBound) {
try {
setJaxbContext(JAXBContext.newInstance(classesToBeBound));
// Every time we set the JAXBContext, we need to also set the marshaller and unmarshaller for EXICodec
getExiCodec().setUnmarshaller(getJaxbContext().createUnmarshaller());
getExiCodec().setMarshaller(getJaxbContext().createMarshaller());
/*
* JAXB by default silently ignores errors. Adding this code to throw an exception if
* something goes wrong.
*/
getExiCodec().getUnmarshaller().setEventHandler(
new ValidationEventHandler() {
@Override
public boolean handleEvent(ValidationEvent event ) {
throw new RuntimeException(event.getMessage(),
event.getLinkedException());
}
});
} catch (JAXBException e) {
getLogger().error("A JAXBException occurred while trying to set JAXB context", e);
}
}
示例2: reportError
import javax.xml.bind.ValidationEventHandler; //导入依赖的package包/类
public void reportError( ValidationEvent ve ) throws SAXException {
ValidationEventHandler handler;
try {
handler = marshaller.getEventHandler();
} catch( JAXBException e ) {
throw new SAXException2(e);
}
if(!handler.handleEvent(ve)) {
if(ve.getLinkedException() instanceof Exception)
throw new SAXException2((Exception)ve.getLinkedException());
else
throw new SAXException2(ve.getMessage());
}
}
示例3: handleEvent
import javax.xml.bind.ValidationEventHandler; //导入依赖的package包/类
/**
* Reports an error to the user, and asks if s/he wants
* to recover. If the canRecover flag is false, regardless
* of the client instruction, an exception will be thrown.
*
* Only if the flag is true and the user wants to recover from an error,
* the method returns normally.
*
* The thrown exception will be catched by the unmarshaller.
*/
public void handleEvent(ValidationEvent event, boolean canRecover ) throws SAXException {
ValidationEventHandler eventHandler = parent.getEventHandler();
boolean recover = eventHandler.handleEvent(event);
// if the handler says "abort", we will not return the object
// from the unmarshaller.getResult()
if(!recover) aborted = true;
if( !canRecover || !recover )
throw new SAXParseException2( event.getMessage(), locator,
new UnmarshalException(
event.getMessage(),
event.getLinkedException() ) );
}
示例4: marshallerFor
import javax.xml.bind.ValidationEventHandler; //导入依赖的package包/类
@Nonnull
private static javax.xml.bind.Marshaller marshallerFor(@Nonnull TestSuites element) {
final javax.xml.bind.Marshaller marshaller;
try {
marshaller = JAXB_CONTEXT.createMarshaller();
marshaller.setProperty(JAXB_FORMATTED_OUTPUT, true);
marshaller.setEventHandler(new ValidationEventHandler() {
@Override
public boolean handleEvent(ValidationEvent event) {
return true;
}
});
} catch (final Exception e) {
throw new RuntimeException("Could not create marshaller to marshall " + element + ".", e);
}
return marshaller;
}
示例5: loadCredentialsList
import javax.xml.bind.ValidationEventHandler; //导入依赖的package包/类
static UserCredentialsList loadCredentialsList(File credentialsFile, ValidationEventHandler validationEventHandler) throws Exception {
if (credentialsFile.exists()) {
final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
final Schema schema = schemaFactory.newSchema(UserCredentialsList.class.getResource(CREDENTIALS_XSD));
final Unmarshaller unmarshaller = JAXB_CONTEXT.createUnmarshaller();
unmarshaller.setSchema(schema);
unmarshaller.setEventHandler(validationEventHandler);
final JAXBElement<UserCredentialsList> element = unmarshaller.unmarshal(new StreamSource(credentialsFile),
UserCredentialsList.class);
UserCredentialsList credentialsList = element.getValue();
return credentialsList;
} else {
final String notFoundMessage = "The credentials configuration file was not found at: " +
credentialsFile.getAbsolutePath();
throw new FileNotFoundException(notFoundMessage);
}
}
示例6: write
import javax.xml.bind.ValidationEventHandler; //导入依赖的package包/类
public static File write(DynamicVRPREPModel dynamicVRPREPModel, Path outputPath) throws JAXBException, SAXException {
outputPath.getParent().toFile().mkdirs();
InputStream stream = Instance.class.getResourceAsStream("/xsd/instance.xsd");
Source schemaSource = new StreamSource(stream);
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(schemaSource);
JAXBContext jc = JAXBContext.newInstance(DynamicVRPREPModel.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setSchema(schema);
marshaller.setEventHandler(new ValidationEventHandler() {
public boolean handleEvent(ValidationEvent event) {
System.err.println("MESSAGE: " + event.getMessage());
return true;
}
});
marshaller.marshal(dynamicVRPREPModel, outputPath.toFile());
return outputPath.toFile();
}
示例7: writeToFile
import javax.xml.bind.ValidationEventHandler; //导入依赖的package包/类
public File writeToFile(Path outputPath) throws JAXBException, SAXException {
outputPath.getParent().toFile().mkdirs();
InputStream stream = Instance.class.getResourceAsStream("/xsd/instance.xsd");
Source schemaSource = new StreamSource(stream);
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(schemaSource);
JAXBContext jc = JAXBContext.newInstance(ResultData.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setSchema(schema);
marshaller.setEventHandler(new ValidationEventHandler() {
public boolean handleEvent(ValidationEvent event) {
System.err.println("MESSAGE: " + event.getMessage());
return true;
}
});
marshaller.marshal(this, outputPath.toFile());
return outputPath.toFile();
}
示例8: createUnmarshaller
import javax.xml.bind.ValidationEventHandler; //导入依赖的package包/类
protected Unmarshaller createUnmarshaller() throws JAXBException, SAXException, FileNotFoundException,
MalformedURLException {
Unmarshaller unmarshaller = getContext().createUnmarshaller();
if (schema != null) {
unmarshaller.setSchema(cachedSchema);
unmarshaller.setEventHandler(new ValidationEventHandler() {
public boolean handleEvent(ValidationEvent event) {
// stop unmarshalling if the event is an ERROR or FATAL
// ERROR
return event.getSeverity() == ValidationEvent.WARNING;
}
});
}
return unmarshaller;
}
示例9: getUnmarshaller
import javax.xml.bind.ValidationEventHandler; //导入依赖的package包/类
/**
* creates a unmarshaller on pooled JAXBContext instances.
* @param jaxbContext the context on which to create a unmarshaller.
* @param validationEventHandler validation event handler.
* @return the unmarshaller.
* @throws JAXBException when unable to create the unmarshaller.
*/
private static Unmarshaller getUnmarshaller(String jaxbContext, ValidationEventHandler validationEventHandler) throws JAXBException {
JAXBContext context = null;
synchronized (s_context) {
context = s_context.get(jaxbContext);
if (null == context) {
context = JAXBContext.newInstance(jaxbContext);
s_context.put(jaxbContext, context);
}
}
Unmarshaller unmarshaller = null;
synchronized (context) {
unmarshaller = context.createUnmarshaller();
}
unmarshaller.setEventHandler(validationEventHandler);
return unmarshaller;
}
示例10: load
import javax.xml.bind.ValidationEventHandler; //导入依赖的package包/类
public static void load() throws JAXBException {
try {
JAXBContext context = JAXBContext.newInstance(Settings.class);
Unmarshaller um = context.createUnmarshaller();
um.setEventHandler(new ValidationEventHandler() {
public boolean handleEvent(ValidationEvent event) {
log.warn("Problem on loading settings.xml: " + event.getMessage());
return true;
}
});
instance = (Settings) um.unmarshal(FILE);
instance.wgsGrid.checkValues();
instance.paperAtlas.checkValues();
SETTINGS_LAST_MODIFIED = FILE.lastModified();
// Settings 重新加载之后,必须更新语言资源
I18nUtils.updateLocalizedStringFormSettings();
} finally {
Settings s = getInstance();
s.applyProxySettings();
}
}
示例11: unmarshalProcessModel
import javax.xml.bind.ValidationEventHandler; //导入依赖的package包/类
public ProcessModel unmarshalProcessModel(File procSchmInFile) throws JAXBException, PropertyException, FileNotFoundException,
IOException {
String pkgName = ProcessModel.class.getCanonicalName().toString();
pkgName = pkgName.substring(0, pkgName.lastIndexOf('.'));
JAXBContext jaxbCtx = JAXBContext.newInstance(pkgName);
Unmarshaller unmarsh = jaxbCtx.createUnmarshaller();
unmarsh.setEventHandler(
new ValidationEventHandler() {
public boolean handleEvent(ValidationEvent event) {
throw new RuntimeException(event.getMessage(),
event.getLinkedException());
}
});
ProcessModel proMod = (ProcessModel) unmarsh.unmarshal(procSchmInFile);
MetaConstraintUtils.createHierarchicalLinks(proMod.getAllConstraints());
return proMod;
}
示例12: convertTo
import javax.xml.bind.ValidationEventHandler; //导入依赖的package包/类
/**
* Converts the given xml into the corresponding T object.
*
* @param xml
* @param context
* @param validationSchema
* @param validationEventHandler
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T convertTo(byte[] xml, JAXBContext context, Schema validationSchema, ValidationEventHandler validationEventHandler)
{
try (ByteArrayInputStream bis = new ByteArrayInputStream(xml);)
{
Unmarshaller unmarshaller = context.createUnmarshaller();
if (validationSchema != null)
unmarshaller.setSchema(validationSchema);
if (validationEventHandler != null)
unmarshaller.setEventHandler(validationEventHandler);
return (T) unmarshaller.unmarshal(bis);
}
catch (JAXBException | IOException e)
{
LOG.error("Failed to unmarshall ", e);
return null;
}
}
示例13: convertToString
import javax.xml.bind.ValidationEventHandler; //导入依赖的package包/类
/**
* Converts the given xml object into the corresponding a string
* representation.
*
* @param xmlObject
* @param context
* @param validationSchema
* @param validationEventHandler
* @return
*/
public static <T> String convertToString(T xmlObject, JAXBContext context, Schema validationSchema, ValidationEventHandler validationEventHandler)
{
try (ByteArrayOutputStream baos = new ByteArrayOutputStream(1024))
{
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
if (validationSchema != null)
marshaller.setSchema(validationSchema);
if (validationEventHandler != null)
marshaller.setEventHandler(validationEventHandler);
marshaller.marshal(xmlObject, baos);
return baos.toString().replaceAll(XMLNS_REGEXP, "");// FIXME remove
// xmlns attr
}
catch (JAXBException | IOException e)
{
LOG.error("Failed to unmarshall ", e);
return null;
}
}
示例14: setProperty
import javax.xml.bind.ValidationEventHandler; //导入依赖的package包/类
public void setProperty(String prop, Object value) {
if (prop.equals(JAXBDataBinding.UNWRAP_JAXB_ELEMENT)) {
unwrapJAXBElement = Boolean.TRUE.equals(value);
} else if (prop.equals(org.apache.cxf.message.Message.class.getName())) {
org.apache.cxf.message.Message m = (org.apache.cxf.message.Message)value;
veventHandler = (ValidationEventHandler)m.getContextualProperty("jaxb-validation-event-handler");
if (veventHandler == null) {
veventHandler = databinding.getValidationEventHandler();
}
setEventHandler = MessageUtils.getContextualBoolean(m, "set-jaxb-validation-event-handler", true);
Object unwrapProperty = m.get(JAXBDataBinding.UNWRAP_JAXB_ELEMENT);
if (unwrapProperty == null) {
unwrapProperty = m.getExchange().get(JAXBDataBinding.UNWRAP_JAXB_ELEMENT);
}
if (unwrapProperty != null) {
unwrapJAXBElement = Boolean.TRUE.equals(unwrapProperty);
}
}
}
示例15: and
import javax.xml.bind.ValidationEventHandler; //导入依赖的package包/类
/**
* Create an instance of {@link IValidationEventHandler} that invokes both
* passed event handlers.
*
* @param aOne
* The first event handler. May be <code>null</code>.
* @param aOther
* The second event handler. May be <code>null</code>.
* @return Never <code>null</code>.
* @since 8.6.0
*/
@Nonnull
static IValidationEventHandler and (@Nullable final ValidationEventHandler aOne,
@Nullable final ValidationEventHandler aOther)
{
if (aOne != null)
{
if (aOther != null)
return x -> {
if (!aOne.handleEvent (x))
{
// We should not continue
return false;
}
return aOther.handleEvent (x);
};
return x -> aOne.handleEvent (x);
}
if (aOther != null)
return x -> aOther.handleEvent (x);
return x -> true;
}