Example usage for javax.xml.bind ValidationException ValidationException

List of usage examples for javax.xml.bind ValidationException ValidationException

Introduction

In this page you can find the example usage for javax.xml.bind ValidationException ValidationException.

Prototype

public ValidationException(Throwable exception) 

Source Link

Document

Construct an ValidationException with a linkedException.

Usage

From source file:com.hp.mqm.clt.RestClient.java

public long postTestResult(HttpEntity entity) throws IOException, ValidationException {
    HttpPost request = new HttpPost(createWorkspaceApiUri(URI_TEST_RESULT_PUSH, settings.isSkipErrors()));
    request.setEntity(entity);/* ww  w  . j a  v  a 2s. c  o  m*/
    CloseableHttpResponse response = null;
    try {
        response = execute(request);
        if (response.getStatusLine().getStatusCode() != HttpStatus.SC_ACCEPTED) {
            String json = IOUtils.toString(response.getEntity().getContent(), "UTF-8");
            JSONObject jsonObject = new JSONObject(json);
            String description = "";
            if (jsonObject.has("description")) {
                description = ": " + jsonObject.getString("description");
            }
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_BAD_REQUEST
                    && settings.isInternal()) {
                // Error was probably caused by XSD validation failure
                throw new ValidationException("Test result XML was refused by server" + description);
            }
            throw new RuntimeException("Test result post failed with status code ("
                    + response.getStatusLine().getStatusCode() + ")" + description);
        }
        String json = IOUtils.toString(response.getEntity().getContent());
        JSONObject jsonObject = new JSONObject(json);
        return jsonObject.getLong("id");
    } finally {
        HttpClientUtils.closeQuietly(response);
    }
}

From source file:org.opennms.netmgt.provision.persist.requisition.Requisition.java

/**
 * Make sure that no data in the requisition is inconsistent.  Nodes should be unique,
 * interfaces should be unique per node, etc.
 */// w  w w.  j a va  2  s.  co  m
public void validate() throws ValidationException {
    final Map<String, Integer> foreignSourceCounts = new HashMap<String, Integer>();
    final Set<String> errors = new HashSet<String>();

    for (final RequisitionNode node : m_nodes) {
        final String foreignId = node.getForeignId();
        Integer count = foreignSourceCounts.get(foreignId);
        foreignSourceCounts.put(foreignId, count == null ? 1 : ++count);
    }

    for (final Entry<String, Integer> entry : foreignSourceCounts.entrySet()) {
        final String foreignId = entry.getKey();
        final Integer count = entry.getValue();
        if (count > 1) {
            errors.add(foreignId + " (" + count + " found)");
        }
    }

    if (errors.size() > 0) {
        final StringBuilder sb = new StringBuilder();
        sb.append("Duplicate nodes found on foreign source ").append(getForeignSource()).append(": ");
        final Iterator<String> it = errors.iterator();
        while (it.hasNext()) {
            final String error = it.next();
            sb.append(error);
            if (it.hasNext()) {
                sb.append(", ");
            }
        }
        throw new ValidationException(sb.toString());
    }
}

From source file:org.pepstock.jem.ant.validator.transformer.TransformerValidator.java

/**
 * Inits the transformer, load the xslt and validate it, load jem properties
 * During the transformation, the transformer onbject is locked, so the file
 * listner is inhibited to do a refresh.
 * /*from   w w  w  . j ava 2  s.co m*/
 * @param xsltvalidatorFile the xslt file
 * @return the transformer
 * @throws ValidationException the validation exception
 */
private Transformer createTransformer(String xsltvalidatorFile) throws ValidationException {

    Transformer t = null;

    // Instantiate a TransformerFactory
    TransformerFactory tFactory = TransformerFactory.newInstance();

    // add error listner to capture validation error.
    ErrorListener tfel = new TransformerFactoryErrorListener();
    tFactory.setErrorListener(tfel);

    // check the transformer compliant
    if (!tFactory.getFeature(SAXTransformerFactory.FEATURE)) {
        throw new ValidationException(AntMessage.JEMA050E.toMessage().getFormattedMessage());
    }

    // activate xalan extension NodeInfo to map source xml code position
    tFactory.setAttribute(TransformerFactoryImpl.FEATURE_SOURCE_LOCATION, Boolean.TRUE);

    StreamSource ss = new StreamSource(xsltvalidatorFile);
    try {
        // A Transformer may be used multiple times.
        // Parameters and output properties are preserved across
        // transformations.
        t = tFactory.newTransformer(ss);
    } catch (TransformerConfigurationException e) {
        throw new ValidationException(AntMessage.JEMA047E.toMessage().getFormattedMessage(e.getMessage()), e);
    }

    // add custom error listener, necessary to avoid internal catch
    // of exception throwed by xslt
    ErrorListener el = new TransformerErrorListener();
    t.setErrorListener(el);

    // pass the parameter list to the xslt
    for (Object key : System.getProperties().keySet()) {
        String keyString = key.toString();
        String value = System.getProperty(keyString);
        t.setParameter(keyString, value);
    }
    for (String key : System.getenv().keySet()) {
        String value = System.getenv().get(key);
        t.setParameter(key, value);
    }

    return t;
}

From source file:org.pepstock.jem.ant.validator.transformer.TransformerValidator.java

/**
 * Validate the AntJcl using a custom xslt.
 * /* www.ja  va2 s  . co  m*/
 * @param jcl the jcl
 * @throws ValidationException the validation exception
 */
public void validate(Jcl jcl) throws ValidationException {

    if (jcl == null) {
        throw new ValidationException("the jcl to validate is null");
    }

    transform(jcl.getContent());

}

From source file:org.pepstock.jem.ant.validator.transformer.TransformerValidator.java

/**
 * Do the xsl transformation. During the transformation, the transformer
 * object is locked, so the file listner is inhibited to do a refresh of
 * transformer object/*from  ww w . j av a2s .co  m*/
 * 
 * @param inXmlContent the in xml content
 * @throws ValidationException the validation exception
 */
private synchronized void transform(String inXmlContent) throws ValidationException {

    // sync is necessary to avoid concurrent access to transformer by file
    // listner
    synchronized (this) {
        StreamSource source = null;
        StreamResult result = null;

        try {
            // traverse the transformation, if some errors are found will be
            // generated some exceptions, otherwise do nothing
            InputStream is = new ByteArrayInputStream(inXmlContent.getBytes(CharSet.DEFAULT));
            source = new StreamSource(is);
            // Use the Transformer to transform an XML Source and send the
            // output to a Result object.
            StringWriter sw = new StringWriter();
            result = new StreamResult(sw);
            transformer.transform(source, result);
            sw.close();

        } catch (TransformerConfigurationException e) {
            throw new ValidationException(e);
        } catch (TransformerException e) {
            throw new ValidationException(e.getMessageAndLocation(), e);
        } catch (Exception e) {
            throw new ValidationException(e);
        }
    }
}

From source file:org.pepstock.jem.ant.validator.transformer.TransformerValidator.java

/**
 * Verify xslt file.//w w w.ja va 2  s  .c om
 * 
 * @param file the file
 * @throws ValidationException the validation exception
 */
public void verifyXsltFile(String file) throws ValidationException {
    if (file == null || file.isEmpty()) {
        throw new ValidationException(AntMessage.JEMA049E.toMessage().getFormattedMessage("null or empty"));
    }

    File f = new File(file);
    if (!f.exists()) {
        throw new ValidationException(AntMessage.JEMA048E.toMessage().getFormattedMessage(file));
    }

    if (!f.isFile()) {
        throw new ValidationException(AntMessage.JEMA049E.toMessage().getFormattedMessage(file));
    }

    try {
        xsltFile = f.getCanonicalPath();
    } catch (IOException e) {
        throw new ValidationException(e);
    }
}