Example usage for org.dom4j DocumentException DocumentException

List of usage examples for org.dom4j DocumentException DocumentException

Introduction

In this page you can find the example usage for org.dom4j DocumentException DocumentException.

Prototype

public DocumentException(Throwable cause) 

Source Link

Usage

From source file:gr.omadak.leviathan.asp.objects.XmlObjectParser.java

License:Open Source License

private void configureMethodMap(ASPMethodMap mMap, Element map, Map innerClasses) throws DocumentException {
    List keys = map.selectNodes("key");
    for (Iterator it = keys.iterator(); it.hasNext();) {
        Element keyEl = (Element) it.next();
        String key = keyEl.attributeValue("value");
        boolean isNStr = false;
        boolean isDefault = false;
        if (key == null) {
            isNStr = Boolean.valueOf(keyEl.attributeValue("nstr")).booleanValue();
            isDefault = !isNStr;//from w w  w  . j  a va  2 s .c o  m
        }
        if (keyEl.attributeValue("use") != null) {
            if (isNStr || isDefault) {
                throw new DocumentException("use attribute can be defined for String cases only");
            }
            ASPClass useClass = getUseClass(keyEl.attributeValue("use"), innerClasses);
            if (useClass == null) {
                throw new DocumentException("Failed to resolve class:" + keyEl.attributeValue("use"));
            }
            mMap.addCase(key, useClass);
        } else {
            AST trans = translate(keyEl, null, false, 0);
            if (trans == null) {
                throw new DocumentException(url + " : Defined null ast for key:" + key);
            } else {
                if (isDefault) {
                    mMap.setDefaultCase(trans);
                } else if (isNStr) {
                    mMap.setNonStringCase(trans);
                } else {
                    mMap.addCase(key, trans);
                }
            }
        }
    }
    mMap.setCaseSensitive(Boolean.valueOf(map.attributeValue("casesensitive")).booleanValue());
}

From source file:gr.omadak.leviathan.asp.objects.XmlObjectParser.java

License:Open Source License

private void assertValue(String value, String msg) throws DocumentException {
    if (value == null) {
        throw new DocumentException(url + ":" + msg);
    }/*from  w ww  .ja  v  a  2s.  c o  m*/
}

From source file:gr.omadak.leviathan.asp.objects.XmlObjectParser.java

License:Open Source License

private int getType(String name) throws DocumentException {
    Integer iType = (Integer) types.get(name);
    if (iType == null) {
        if (registeredObjects != null && registeredObjects.containsKey(name.toUpperCase())) {
            return objectType;
        } else {//from  w w w. j ava 2s. c  o  m
            throw new DocumentException(url + " : code not found for type:" + name);
        }
    }
    return iType.intValue();
}

From source file:gr.omadak.leviathan.asp.objects.XmlObjectParser.java

License:Open Source License

private void configureProperty(GenericClass clazz, Element propertyEl, Map innerClasses)
        throws DocumentException {
    lastArgType = -1;/*from www .j  a  v  a  2  s.  c  o  m*/
    String propName = propertyEl.attributeValue("name");
    assertValue(propName, "Name not specified for property");
    String use = propertyEl.attributeValue("use");
    Property property = null;
    boolean isDefault = Boolean.valueOf(propertyEl.attributeValue("default")).booleanValue();
    lastState = null;
    if (use != null) {
        ASPDependantClass aspClazz = (ASPDependantClass) getUseClass(use, innerClasses);
        if (aspClazz != null) {
            property = new ASPPropertyWrapper(aspClazz);
            ((ASPPropertyWrapper) property).setName(propName);
        }
    } else {
        String type = propertyEl.attributeValue("type");
        assertValue(type, "Property type not specified");
        int dType;
        ASPClass retClass = null;
        try {
            dType = getType(type);
        } catch (DocumentException de) {
            dType = -1;
        }
        if (dType == objectType || dType == -1) {
            retClass = getUseClass(type, innerClasses);
            if (retClass == null) {
                throw new DocumentException("type:" + type + " is not recognized and is not a known class");
            }
            dType = objectType;
        }
        Element read = propertyEl.element("read");
        AST readAST = null;
        AST writeAST = null;
        if (read != null) {
            readAST = translate(read, null, false, 0);
        }
        Element write = propertyEl.element("write");
        if (write != null) {
            writeAST = translate(write, null, true, 0);
        }
        property = new GenericASPProperty(propName, readAST, writeAST, dType);
        if (retClass != null) {
            property.setRetObjectClass(retClass);
        }
        if (lastArgType != -1) {
            property.setArgType(lastArgType);
        }
    }
    if (property != null) {
        if (lastState != null) {
            if (idMethods == null) {
                idMethods = new HashMap();
                idMethods.put(lastState, new Object[] { null, property, new Integer(lastIndex) });
            } else {
                Object[] obj = (Object[]) idMethods.get(lastState);
                obj[1] = property;
                obj[2] = new Integer(lastIndex);
            }
        }
        lastState = null;
        if (isDefault) {
            clazz.setDefaultProperty(property);
        } else {
            clazz.addMember(property);
        }
        configureInclude(property, propertyEl);
    } else {
        throw new DocumentException(url + " : Failed to create Property:" + propertyEl.getText());
    }
}

From source file:gr.omadak.leviathan.asp.objects.XmlObjectParser.java

License:Open Source License

private AST translate(Element el, AST root, boolean evalFirstLevelArgs, int cDepth) throws DocumentException {
    List astChildren = new ArrayList();
    boolean firstAST = cDepth > 0;
    for (Iterator it = el.elementIterator(); it.hasNext();) {
        Element ch = (Element) it.next();
        if ("ast".equals(ch.getName())) {
            firstAST = true;/*w  w  w  . j a v  a 2s. com*/
            String type = ch.attributeValue("name");
            String text = ch.attributeValue("text");
            assertValue(type, "Name of ast elements is required");
            text = text == null ? type : text;
            int dType = getType(type);
            AST node = createNode(dType, text);
            if (ch.elementIterator().hasNext()) {
                translate(ch, node, evalFirstLevelArgs, cDepth + 1);
            }
            astChildren.add(node);
        } else if ("arg".equals(ch.getName())) {
            if (root == null) {
                if (evalFirstLevelArgs) {
                    String type = ch.attributeValue("type");
                    assertValue(type, "Type of arg is required");
                    int objType = getType(type);
                    lastArgType = objType;
                }
            } else {
                String index = ch.attributeValue("index");
                assertValue(index, "Argument index should be specified");
                try {
                    Integer.parseInt(index);
                } catch (NumberFormatException nfe) {
                    throw new DocumentException(url + " : Invalid value:" + index + " for attribute index");
                }
                astChildren.add(createNode(astArgType, index));
            }
        } else if ("args".equals(ch.getName())) {
            if (firstAST) {
                astChildren.add(ch.attributeValue("mode") == null ? factory.create(CommonConstants.ALL_ARGS)
                        : createNode(CommonConstants.ALL_ARGS, ch.attributeValue("mode")));
            }
        } else if ("this".equals(ch.getName())) {
            astChildren.add(createNode(CommonConstants.INSTANCE, "this"));
        } else if ("state".equals(ch.getName())) {
            String id = ch.attributeValue("methodId");
            assertValue(id, "The id of the method affected is not defined");
            String index = ch.attributeValue("index");
            assertValue(index, "The index of the argument for the affected method " + "is not defined");
            try {
                lastIndex = Integer.parseInt(index) - 1;
            } catch (NumberFormatException nfe) {
                throw new DocumentException("The index for the affected mathod is invalid:" + index);
            }
            lastState = id;
        } else {
            System.err.println("Unexpected element:" + ch.getName());
        }
    }
    if (astChildren.isEmpty()) {
        return null;
    }
    if (root == null && astChildren.size() > 1) {
        root = createNode(translateRoot, "TRANSLATE_ROOT");
    }
    AST current = null;
    for (Iterator it = astChildren.iterator(); it.hasNext();) {
        if (root == null) {
            root = (AST) it.next();
        } else if (root.getFirstChild() == null) {
            current = (AST) it.next();
            root.setFirstChild(current);
        } else {
            if (current == null) {
                current = root.getFirstChild();
            }
            AST next = (AST) it.next();
            current.setNextSibling(next);
            current = next;
        }
    }
    return root;
}

From source file:nl.knaw.dans.common.lang.xml.Dom4jReader.java

License:Apache License

public Node getNode(String xpath) throws DocumentException {
    Node node = document.selectSingleNode(xpath);
    if (node == null) {
        throw new DocumentException("The xpath expression '" + xpath + "' did not yield a result.");
    }/*  ww  w.j a va 2s  . com*/
    return node;
}

From source file:nl.tue.gale.common.XPPEntityReader.java

License:Open Source License

protected Document parseDocument() throws DocumentException, IOException, XmlPullParserException {
    DocumentFactory df = getDocumentFactory();
    Document document = df.createDocument();
    Element parent = null;/*from ww w. j  a  v  a2  s .  co  m*/
    XmlPullParser pp = getXPPParser();
    pp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
    pp.setFeature(XmlPullParser.FEATURE_PROCESS_DOCDECL, false);
    defineEntities(pp);

    while (true) {
        int type = pp.nextToken();

        switch (type) {
        case XmlPullParser.PROCESSING_INSTRUCTION: {
            String text = pp.getText();
            int loc = text.indexOf(" ");

            if (loc >= 0) {
                String target = text.substring(0, loc);
                String txt = text.substring(loc + 1);
                document.addProcessingInstruction(target, txt);
            } else {
                document.addProcessingInstruction(text, "");
            }

            break;
        }

        case XmlPullParser.COMMENT: {
            if (parent != null) {
                parent.addComment(pp.getText());
            } else {
                document.addComment(pp.getText());
            }

            break;
        }

        case XmlPullParser.CDSECT: {
            if (parent != null) {
                parent.addCDATA(pp.getText());
            } else {
                String msg = "Cannot have text content outside of the " + "root document";
                throw new DocumentException(msg);
            }

            break;
        }

        case XmlPullParser.ENTITY_REF:
            if (parent != null) {
                if (pp.getName().equals("gt")) {
                    parent.addText(">");
                } else if (pp.getName().equals("lt")) {
                    parent.addText("<");
                } else if (pp.getName().equals("amp")) {
                    parent.addText("&");
                } else if (pp.getName().equals("quot")) {
                    parent.addText("\"");
                } else
                    parent.addEntity(pp.getName(), "&" + pp.getName() + ";");
            }
            break;

        case XmlPullParser.END_DOCUMENT:
            return document;

        case XmlPullParser.START_TAG: {
            QName qname = (pp.getPrefix() == null) ? df.createQName(pp.getName(), pp.getNamespace())
                    : df.createQName(pp.getName(), pp.getPrefix(), pp.getNamespace());
            Element newElement = df.createElement(qname);
            int nsStart = pp.getNamespaceCount(pp.getDepth() - 1);
            int nsEnd = pp.getNamespaceCount(pp.getDepth());

            for (int i = nsStart; i < nsEnd; i++) {
                if (pp.getNamespacePrefix(i) != null) {
                    newElement.addNamespace(pp.getNamespacePrefix(i), pp.getNamespaceUri(i));
                }
            }

            for (int i = 0; i < pp.getAttributeCount(); i++) {
                QName qa = (pp.getAttributePrefix(i) == null) ? df.createQName(pp.getAttributeName(i))
                        : df.createQName(pp.getAttributeName(i), pp.getAttributePrefix(i),
                                pp.getAttributeNamespace(i));
                newElement.addAttribute(qa, pp.getAttributeValue(i));
            }

            if (parent != null) {
                parent.add(newElement);
            } else {
                document.add(newElement);
            }

            parent = newElement;

            break;
        }

        case XmlPullParser.END_TAG: {
            if (parent != null) {
                parent = parent.getParent();
            }

            break;
        }

        case XmlPullParser.TEXT: {
            String text = pp.getText();

            if (parent != null) {
                parent.addText(text);
            } else {
                String msg = "Cannot have text content outside of the " + "root document";
                throw new DocumentException(msg);
            }

            break;
        }

        default:
            break;
        }
    }
}

From source file:org.codehaus.aspectwerkz.definition.XmlParser.java

License:Open Source License

/**
 * Creates a DOM document.//  w  w  w .  ja va2s  .co  m
 *
 * @param url the URL to the file containing the XML
 * @return the DOM document
 * @throws DocumentException
 */
public static Document createDocument(final URL url) throws DocumentException {
    SAXReader reader = new SAXReader();
    setEntityResolver(reader);
    InputStream in = null;
    try {
        in = url.openStream();
        return reader.read(in);
    } catch (IOException e) {
        throw new DocumentException(e);
    } finally {
        try {
            in.close();
        } catch (Throwable t) {
            ;
        }
    }
}

From source file:org.guzz.builder.HbmXMLBuilder.java

License:Apache License

public static String getDomainClassName(Element root) throws DocumentException, IOException, SAXException {
    String packageName = root.attributeValue("package");

    List bus = root.selectNodes("//class");

    if (bus == null)
        return null;
    if (bus.size() != 1) {
        throw new DocumentException("too many class name");
    }//from   ww  w .  j av  a 2 s  .  c o m

    Element e = (Element) bus.get(0);

    String className = e.attributeValue("name");
    if (StringUtil.notEmpty(packageName)) {
        className = packageName + "." + className;
    }

    return className;
}

From source file:org.guzz.builder.HbmXMLBuilder.java

License:Apache License

public static String getDomainClassBusinessName(Element root)
        throws DocumentException, IOException, SAXException {
    List bus = root.selectNodes("//class");

    if (bus == null)
        return null;
    if (bus.size() != 1) {
        throw new DocumentException("too many class name");
    }/*from w  w w  . jav  a  2  s .co m*/
    Element e = (Element) bus.get(0);

    return e.attributeValue("businessName");
}