Search in sources :

Example 1 with BuilderException

use of org.apache.ibatis.builder.BuilderException in project mybatis-3 by mybatis.

the class MapperAnnotationBuilder method parseCacheRef.

private void parseCacheRef() {
    CacheNamespaceRef cacheDomainRef = type.getAnnotation(CacheNamespaceRef.class);
    if (cacheDomainRef != null) {
        Class<?> refType = cacheDomainRef.value();
        String refName = cacheDomainRef.name();
        if (refType == void.class && refName.isEmpty()) {
            throw new BuilderException("Should be specified either value() or name() attribute in the @CacheNamespaceRef");
        }
        if (refType != void.class && !refName.isEmpty()) {
            throw new BuilderException("Cannot use both value() and name() attribute in the @CacheNamespaceRef");
        }
        String namespace = (refType != void.class) ? refType.getName() : refName;
        assistant.useCacheRef(namespace);
    }
}
Also used : BuilderException(org.apache.ibatis.builder.BuilderException) CacheNamespaceRef(org.apache.ibatis.annotations.CacheNamespaceRef)

Example 2 with BuilderException

use of org.apache.ibatis.builder.BuilderException in project mybatis-3 by mybatis.

the class ProviderSqlSource method createSqlSource.

private SqlSource createSqlSource(Object parameterObject) {
    try {
        Class<?>[] parameterTypes = providerMethod.getParameterTypes();
        String sql;
        if (parameterTypes.length == 0) {
            sql = (String) providerMethod.invoke(providerType.newInstance());
        } else if (parameterTypes.length == 1 && (parameterObject == null || parameterTypes[0].isAssignableFrom(parameterObject.getClass()))) {
            sql = (String) providerMethod.invoke(providerType.newInstance(), parameterObject);
        } else if (parameterObject instanceof Map) {
            @SuppressWarnings("unchecked") Map<String, Object> params = (Map<String, Object>) parameterObject;
            sql = (String) providerMethod.invoke(providerType.newInstance(), extractProviderMethodArguments(params, providerMethodArgumentNames));
        } else {
            throw new BuilderException("Error invoking SqlProvider method (" + providerType.getName() + "." + providerMethod.getName() + "). Cannot invoke a method that holds " + (parameterTypes.length == 1 ? "named argument(@Param)" : "multiple arguments") + " using a specifying parameterObject. In this case, please specify a 'java.util.Map' object.");
        }
        Class<?> parameterType = parameterObject == null ? Object.class : parameterObject.getClass();
        return sqlSourceParser.parse(sql, parameterType, new HashMap<String, Object>());
    } catch (BuilderException e) {
        throw e;
    } catch (Exception e) {
        throw new BuilderException("Error invoking SqlProvider method (" + providerType.getName() + "." + providerMethod.getName() + ").  Cause: " + e, e);
    }
}
Also used : BuilderException(org.apache.ibatis.builder.BuilderException) Map(java.util.Map) HashMap(java.util.HashMap) BuilderException(org.apache.ibatis.builder.BuilderException)

Example 3 with BuilderException

use of org.apache.ibatis.builder.BuilderException in project mybatis-3 by mybatis.

the class XMLScriptBuilder method parseDynamicTags.

List<SqlNode> parseDynamicTags(XNode node) {
    List<SqlNode> contents = new ArrayList<SqlNode>();
    NodeList children = node.getNode().getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        XNode child = node.newXNode(children.item(i));
        if (child.getNode().getNodeType() == Node.CDATA_SECTION_NODE || child.getNode().getNodeType() == Node.TEXT_NODE) {
            String data = child.getStringBody("");
            TextSqlNode textSqlNode = new TextSqlNode(data);
            if (textSqlNode.isDynamic()) {
                contents.add(textSqlNode);
                isDynamic = true;
            } else {
                contents.add(new StaticTextSqlNode(data));
            }
        } else if (child.getNode().getNodeType() == Node.ELEMENT_NODE) {
            // issue #628
            String nodeName = child.getNode().getNodeName();
            NodeHandler handler = nodeHandlers(nodeName);
            if (handler == null) {
                throw new BuilderException("Unknown element <" + nodeName + "> in SQL statement.");
            }
            handler.handleNode(child, contents);
            isDynamic = true;
        }
    }
    return contents;
}
Also used : BuilderException(org.apache.ibatis.builder.BuilderException) NodeList(org.w3c.dom.NodeList) XNode(org.apache.ibatis.parsing.XNode) ArrayList(java.util.ArrayList)

Example 4 with BuilderException

use of org.apache.ibatis.builder.BuilderException in project mybatis-3 by mybatis.

the class XPathParser method createDocument.

private Document createDocument(InputSource inputSource) {
    // important: this must only be called AFTER common constructor
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(validation);
        factory.setNamespaceAware(false);
        factory.setIgnoringComments(true);
        factory.setIgnoringElementContentWhitespace(false);
        factory.setCoalescing(false);
        factory.setExpandEntityReferences(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setEntityResolver(entityResolver);
        builder.setErrorHandler(new ErrorHandler() {

            @Override
            public void error(SAXParseException exception) throws SAXException {
                throw exception;
            }

            @Override
            public void fatalError(SAXParseException exception) throws SAXException {
                throw exception;
            }

            @Override
            public void warning(SAXParseException exception) throws SAXException {
            }
        });
        return builder.parse(inputSource);
    } catch (Exception e) {
        throw new BuilderException("Error creating document instance.  Cause: " + e, e);
    }
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) BuilderException(org.apache.ibatis.builder.BuilderException) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) BuilderException(org.apache.ibatis.builder.BuilderException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException)

Example 5 with BuilderException

use of org.apache.ibatis.builder.BuilderException in project mybatis-3 by mybatis.

the class MapperAnnotationBuilder method getSqlSourceFromAnnotations.

private SqlSource getSqlSourceFromAnnotations(Method method, Class<?> parameterType, LanguageDriver languageDriver) {
    try {
        Class<? extends Annotation> sqlAnnotationType = getSqlAnnotationType(method);
        Class<? extends Annotation> sqlProviderAnnotationType = getSqlProviderAnnotationType(method);
        if (sqlAnnotationType != null) {
            if (sqlProviderAnnotationType != null) {
                throw new BindingException("You cannot supply both a static SQL and SqlProvider to method named " + method.getName());
            }
            Annotation sqlAnnotation = method.getAnnotation(sqlAnnotationType);
            final String[] strings = (String[]) sqlAnnotation.getClass().getMethod("value").invoke(sqlAnnotation);
            return buildSqlSourceFromStrings(strings, parameterType, languageDriver);
        } else if (sqlProviderAnnotationType != null) {
            Annotation sqlProviderAnnotation = method.getAnnotation(sqlProviderAnnotationType);
            return new ProviderSqlSource(assistant.getConfiguration(), sqlProviderAnnotation);
        }
        return null;
    } catch (Exception e) {
        throw new BuilderException("Could not find value method on SQL annotation.  Cause: " + e, e);
    }
}
Also used : BuilderException(org.apache.ibatis.builder.BuilderException) Annotation(java.lang.annotation.Annotation) IncompleteElementException(org.apache.ibatis.builder.IncompleteElementException) BindingException(org.apache.ibatis.binding.BindingException) IOException(java.io.IOException) BuilderException(org.apache.ibatis.builder.BuilderException) BindingException(org.apache.ibatis.binding.BindingException)

Aggregations

BuilderException (org.apache.ibatis.builder.BuilderException)14 Properties (java.util.Properties)6 XNode (org.apache.ibatis.parsing.XNode)4 IncompleteElementException (org.apache.ibatis.builder.IncompleteElementException)2 NodeList (org.w3c.dom.NodeList)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 CacheNamespaceRef (org.apache.ibatis.annotations.CacheNamespaceRef)1 BindingException (org.apache.ibatis.binding.BindingException)1 DataSourceFactory (org.apache.ibatis.datasource.DataSourceFactory)1 MetaClass (org.apache.ibatis.reflection.MetaClass)1 TransactionFactory (org.apache.ibatis.transaction.TransactionFactory)1 Node (org.w3c.dom.Node)1 ErrorHandler (org.xml.sax.ErrorHandler)1