本文整理匯總了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;
}