Example #1
0
  public static Object nodeToObjectModel(Node n, Class declaredType) throws Docx4JException {

    if (n == null) {
      throw new Docx4JException("null input");
    }

    //    	if (log.isDebugEnabled() ) {
    //    		System.out.println("IN: " + XmlUtils.w3CDomNodeToString(n));
    //    	}

    Object jaxb = null;
    try {
      jaxb = XmlUtils.unmarshal(n, Context.jcPML, declaredType);
    } catch (JAXBException e1) {
      throw new Docx4JException("Couldn't unmarshall " + XmlUtils.w3CDomNodeToString(n), e1);
    }
    try {
      if (jaxb instanceof JAXBElement) {

        JAXBElement jb = (JAXBElement) jaxb;
        if (jb.getDeclaredType().getName().equals(declaredType.getName())) {
          return jb.getValue();
        } else {
          log.error("UNEXPECTED " + XmlUtils.JAXBElementDebug(jb));
          throw new Docx4JException(
              "Expected " + declaredType.getName() + " but got " + XmlUtils.JAXBElementDebug(jb));
        }
      } else if (jaxb.getClass().getName().equals(declaredType.getName())) {
        return jaxb;
      } else {
        log.error(jaxb.getClass().getName());
        throw new Docx4JException(
            "Expected " + declaredType.getName() + " but got " + jaxb.getClass().getName());
      }
    } catch (ClassCastException e) {
      throw new Docx4JException(
          "Expected " + declaredType.getName() + " but got " + jaxb.getClass().getName(), e);
    }
  }