Search in sources :

Example 1 with EntityHandler

use of com.agiletec.aps.system.common.entity.parse.EntityHandler in project entando-core by entando.

the class ApsEntityManager method createEntityFromXml.

/**
 * Create and populate the entity as specified by its type and XML
 * definition.
 *
 * @param entityTypeCode The Entity Type code.
 * @param xml The XML of the associated entity.
 * @return The populated entity.
 * @throws ApsSystemException If errors detected while retrieving the
 * entity.
 */
protected IApsEntity createEntityFromXml(String entityTypeCode, String xml) throws ApsSystemException {
    try {
        IApsEntity entityPrototype = this.getEntityPrototype(entityTypeCode);
        SAXParserFactory parseFactory = SAXParserFactory.newInstance();
        SAXParser parser = parseFactory.newSAXParser();
        InputSource is = new InputSource(new StringReader(xml));
        EntityHandler handler = this.getEntityHandler();
        handler.initHandler(entityPrototype, this.getXmlAttributeRootElementName(), this.getCategoryManager());
        parser.parse(is, handler);
        return entityPrototype;
    } catch (ParserConfigurationException | SAXException | IOException t) {
        logger.error("Error detected while creating the entity. typecode: {} - xml: {}", entityTypeCode, xml, t);
        throw new ApsSystemException("Error detected while creating the entity", t);
    }
}
Also used : InputSource(org.xml.sax.InputSource) StringReader(java.io.StringReader) SAXParser(javax.xml.parsers.SAXParser) IApsEntity(com.agiletec.aps.system.common.entity.model.IApsEntity) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) EntityHandler(com.agiletec.aps.system.common.entity.parse.EntityHandler) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Aggregations

IApsEntity (com.agiletec.aps.system.common.entity.model.IApsEntity)1 EntityHandler (com.agiletec.aps.system.common.entity.parse.EntityHandler)1 ApsSystemException (com.agiletec.aps.system.exception.ApsSystemException)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 SAXParser (javax.xml.parsers.SAXParser)1 SAXParserFactory (javax.xml.parsers.SAXParserFactory)1 InputSource (org.xml.sax.InputSource)1 SAXException (org.xml.sax.SAXException)1