private boolean checkRootfile( ZipInputStream zipStream,
                                   ZipEntry entry ) throws Exception {
        List<EpubMetadataProperty> properties = new ArrayList<>();
        Map<String, EpubMetadataProperty> propertiesWithId = new HashMap<>();

        ByteArrayOutputStream content = getZipEntryContent(zipStream, entry);

        // get metadata elements
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(new ByteArrayInputStream(content.toByteArray()));

        XPathFactory xPathfactory = XPathFactory.newInstance();
        XPath xpath = xPathfactory.newXPath();
        xpath.setNamespaceContext(new SimpleNamespaceContext().setNamespace(DUBLIN_CORE_PREFIX, DUBLIN_CORE_URI));
        XPathExpression expr = xpath.compile("//" + DUBLIN_CORE_PREFIX + ":*");
        NodeList metadata = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

        for (int i = 0; i < metadata.getLength(); i++) {
            EpubMetadataProperty property = new EpubMetadataProperty();

            Node node = metadata.item(i);
            property.setName(node.getLocalName());
            property.setValue(node.getTextContent());

            Node nodeId = node.getAttributes().getNamedItem("id");
            if (nodeId != null) {
                String id = nodeId.getTextContent();
                if (!propertiesWithId.containsKey(id)) {
                    propertiesWithId.put(id, property);
                }
            } else {
                properties.add(property);
            }
        }

        // read <meta> elements which can refine the properties
        NodeList metadataRefines = doc.getElementsByTagName("meta");
        for (int i = 0; i < metadataRefines.getLength(); i++) {
            Node node = metadataRefines.item(i);
            Node refines = node.getAttributes().getNamedItem("refines");
            if (refines != null) {
                String refinesId = refines.getTextContent().substring(1);
                if (propertiesWithId.containsKey(refinesId)) {
                    EpubMetadataProperty property = propertiesWithId.get(refinesId);

                    Node schemeAttribute = node.getAttributes().getNamedItem("scheme");
                    if (schemeAttribute != null) {
                        property.setScheme(schemeAttribute.getTextContent());
                    }

                    String propertyName = node.getAttributes().getNamedItem("property").getTextContent();
                    if (propertyName.equals("title-type")) {
                        property.setTitleType(node.getTextContent());
                    } else if (propertyName.equals("identifier-type")) {
                        property.setIdentifierType(node.getTextContent());
                    } else if (propertyName.equals("metadata-authority")) {
                        property.setMetadataAuthority(node.getTextContent());
                    } else if (propertyName.equals("role")) {
                        property.setRole(node.getTextContent());
                    } else if (propertyName.equals("display-seq")) {
                        property.setDisplaySeq(Long.parseLong(node.getTextContent()));
                    } else if (propertyName.equals("group-position")) {
                        property.setGroupPosition(Long.parseLong(node.getTextContent()));
                    } else if (propertyName.equals("file-as")) {
                        property.setFileAs(node.getTextContent());
                    } else if (propertyName.equals("alternate-script")) {
                        Node languageAttribute = node.getAttributes().getNamedItem("xml:lang");
                        if (languageAttribute != null) {
                            property.setAlternateScript(new AlternateScript(node.getTextContent(), languageAttribute.getTextContent()));
                        }
                    }
                }
            }
        }

        properties.addAll(propertiesWithId.values());
        for (EpubMetadataProperty prop : properties) {
            String propertyName = prop.getName();
            if (propertyName.equals("identifier")) {
                getIdentifier().add(prop);
            } else if (propertyName.equals("title")) {
                getTitle().add(prop);
            } else if (propertyName.equals("language")) {
                getLanguage().add(prop);
            } else if (propertyName.equals("contributor")) {
                getContributor().add(prop);
            } else if (propertyName.equals("creator")) {
                getCreator().add(prop);
            } else if (propertyName.equals("description")) {
                getDescription().add(prop);
            } else if (propertyName.equals("publisher")) {
                getPublisher().add(prop);
            } else if (propertyName.equals("rights")) {
                getRights().add(prop);
            } else if (propertyName.equals("date")) {
                getDate().add(prop);
            }
        }

        return true;
    }
private boolean checkRootfile ( ZipInputStream zipStream , ZipEntry entry ) throws Exception { List < EpubMetadataProperty > properties = new ArrayList < > ( ) ; Map < String , EpubMetadataProperty > propertiesWithId = new HashMap < > ( ) ; ByteArrayOutputStream content = getZipEntryContent ( zipStream , entry ) ; DocumentBuilderFactory factory = DocumentBuilderFactory . newInstance ( ) ; factory . setNamespaceAware ( true ) ; DocumentBuilder builder = factory . newDocumentBuilder ( ) ; Document doc = builder . parse ( new ByteArrayInputStream ( content . toByteArray ( ) ) ) ; XPathFactory xPathfactory = XPathFactory . newInstance ( ) ; XPath xpath = xPathfactory . newXPath ( ) ; xpath . setNamespaceContext ( new SimpleNamespaceContext ( ) . setNamespace ( DUBLIN_CORE_PREFIX , DUBLIN_CORE_URI ) ) ; XPathExpression expr = xpath . compile ( "//" + DUBLIN_CORE_PREFIX + ":*" ) ; NodeList metadata = ( NodeList ) expr . evaluate ( doc , XPathConstants . NODESET ) ; for ( int i = 0 ; i < metadata . getLength ( ) ; i ++ ) { EpubMetadataProperty property = new EpubMetadataProperty ( ) ; Node node = metadata . item ( i ) ; property . setName ( node . getLocalName ( ) ) ; property . setValue ( node . getTextContent ( ) ) ; Node nodeId = node . getAttributes ( ) . getNamedItem ( "id" ) ; if ( nodeId != null ) { String id = nodeId . getTextContent ( ) ; if ( ! propertiesWithId . containsKey ( id ) ) { propertiesWithId . put ( id , property ) ; } } else { properties . add ( property ) ; } } NodeList metadataRefines = doc . getElementsByTagName ( "meta" ) ; for ( int i = 0 ; i < metadataRefines . getLength ( ) ; i ++ ) { Node node = metadataRefines . item ( i ) ; Node refines = node . getAttributes ( ) . getNamedItem ( "refines" ) ; if ( refines != null ) { String refinesId = refines . getTextContent ( ) . substring ( 1 ) ; if ( propertiesWithId . containsKey ( refinesId ) ) { EpubMetadataProperty property = propertiesWithId . get ( refinesId ) ; Node schemeAttribute = node . getAttributes ( ) . getNamedItem ( "scheme" ) ; if ( schemeAttribute != null ) { property . setScheme ( schemeAttribute . getTextContent ( ) ) ; } String propertyName = node . getAttributes ( ) . getNamedItem ( "property" ) . getTextContent ( ) ; if ( propertyName . equals ( "title-type" ) ) { property . setTitleType ( node . getTextContent ( ) ) ; } else if ( propertyName . equals ( "identifier-type" ) ) { property . setIdentifierType ( node . getTextContent ( ) ) ; } else if ( propertyName . equals ( "metadata-authority" ) ) { property . setMetadataAuthority ( node . getTextContent ( ) ) ; } else if ( propertyName . equals ( "role" ) ) { property . setRole ( node . getTextContent ( ) ) ; } else if ( propertyName . equals ( "display-seq" ) ) { property . setDisplaySeq ( Long . parseLong ( node . getTextContent ( ) ) ) ; } else if ( propertyName . equals ( "group-position" ) ) { property . setGroupPosition ( Long . parseLong ( node . getTextContent ( ) ) ) ; } else if ( propertyName . equals ( "file-as" ) ) { property . setFileAs ( node . getTextContent ( ) ) ; } else if ( propertyName . equals ( "alternate-script" ) ) { Node languageAttribute = node . getAttributes ( ) . getNamedItem ( "xml:lang" ) ; if ( languageAttribute != null ) { property . setAlternateScript ( new AlternateScript ( node . getTextContent ( ) , languageAttribute . getTextContent ( ) ) ) ; } } } } } properties . addAll ( propertiesWithId . values ( ) ) ; for ( EpubMetadataProperty prop : properties ) { String propertyName = prop . getName ( ) ; if ( propertyName . equals ( "identifier" ) ) { getIdentifier ( ) . add ( prop ) ; } else if ( propertyName . equals ( "title" ) ) { getTitle ( ) . add ( prop ) ; } else if ( propertyName . equals ( "language" ) ) { getLanguage ( ) . add ( prop ) ; } else if ( propertyName . equals ( "contributor" ) ) { getContributor ( ) . add ( prop ) ; } else if ( propertyName . equals ( "creator" ) ) { getCreator ( ) . add ( prop ) ; } else if ( propertyName . equals ( "description" ) ) { getDescription ( ) . add ( prop ) ; } else if ( propertyName . equals ( "publisher" ) ) { getPublisher ( ) . add ( prop ) ; } else if ( propertyName . equals ( "rights" ) ) { getRights ( ) . add ( prop ) ; } else if ( propertyName . equals ( "date" ) ) { getDate ( ) . add ( prop ) ; } } return true ; }
Process the EPUB package and extract the metadata fields.