Example usage for org.w3c.dom Element getAttributeNode

List of usage examples for org.w3c.dom Element getAttributeNode

Introduction

In this page you can find the example usage for org.w3c.dom Element getAttributeNode.

Prototype

public Attr getAttributeNode(String name);

Source Link

Document

Retrieves an attribute node by name.

Usage

From source file:org.apereo.portal.layout.dlm.EditManager.java

/**
   Evaluate whether attribute changes exist in the ilfChild and if so
   apply them. Returns true if some changes existed. If changes existed
   but matched those in the original node then they are not applicable,
   are removed from the editSet, and false is returned.
*//*  ww w .j a v a 2 s  . co m*/
public static boolean applyEditSet(Element plfChild, Element original) {
    // first get edit set if it exists
    Element editSet = null;
    try {
        editSet = getEditSet(plfChild, null, null, false);
    } catch (Exception e) {
        // should never occur unless problem during create in getEditSet
        // and we are telling it not to create.
        return false;
    }

    if (editSet == null || editSet.getChildNodes().getLength() == 0)
        return false;

    if (original.getAttribute(Constants.ATT_EDIT_ALLOWED).equals("false")) {
        // can't change anymore so discard changes
        plfChild.removeChild(editSet);
        return false;
    }

    Document ilf = original.getOwnerDocument();
    boolean attributeChanged = false;
    Element edit = (Element) editSet.getFirstChild();

    while (edit != null) {
        String attribName = edit.getAttribute(Constants.ATT_NAME);
        Attr attr = plfChild.getAttributeNode(attribName);

        // preferences are only updated at preference storage time so
        // if a preference change exists in the edit set assume it is
        // still valid so that the node being edited will persist in
        // the PLF.
        if (edit.getNodeName().equals(Constants.ELM_PREF))
            attributeChanged = true;
        else if (attr == null) {
            // attribute removed. See if needs removing in original.
            attr = original.getAttributeNode(attribName);
            if (attr == null) // edit irrelevant,
                editSet.removeChild(edit);
            else {
                // edit differs, apply to original
                original.removeAttribute(attribName);
                attributeChanged = true;
            }
        } else {
            // attribute there, see if original is also there
            Attr origAttr = original.getAttributeNode(attribName);
            if (origAttr == null) {
                // original attribute isn't defined so need to add
                origAttr = (Attr) ilf.importNode(attr, true);
                original.setAttributeNode(origAttr);
                attributeChanged = true;
            } else {
                // original attrib found, see if different
                if (attr.getValue().equals(origAttr.getValue())) {
                    // they are the same, edit irrelevant
                    editSet.removeChild(edit);
                } else {
                    // edit differs, apply to original
                    origAttr.setValue(attr.getValue());
                    attributeChanged = true;
                }
            }
        }
        edit = (Element) edit.getNextSibling();
    }
    return attributeChanged;
}

From source file:org.apereo.portal.layout.dlm.ParameterEditManager.java

/**
 * Attempt to apply a single channel parameter edit command and return true
 * if it succeeds or false otherwise. If the edit is disallowed or the
 * target element no longer exists in the document the edit command fails
 * and returns false.//from   www  . j a  v  a2  s .c o  m
 * @throws Exception
 */
private static boolean applyEdit(Element edit, Document ilf) {
    String nodeID = edit.getAttribute(Constants.ATT_TARGET);

    Element channel = ilf.getElementById(nodeID);

    if (channel == null)
        return false;

    // now get the name of the parameter to be edited and find that element
    String parmName = edit.getAttribute(Constants.ATT_NAME);
    String parmValue = edit.getAttribute(Constants.ATT_USER_VALUE);
    NodeList ilfParms = channel.getChildNodes();
    Element targetParm = null;

    for (int i = 0; i < ilfParms.getLength(); i++) {
        Element ilfParm = (Element) ilfParms.item(i);
        if (ilfParm.getAttribute(Constants.ATT_NAME).equals(parmName)) {
            targetParm = ilfParm;
            break;
        }
    }
    if (targetParm == null) // parameter not found so we are free to set
    {
        Element parameter = ilf.createElement("parameter");
        parameter.setAttribute("name", parmName);
        parameter.setAttribute("value", parmValue);
        parameter.setAttribute("override", "yes");
        channel.appendChild(parameter);
        return true;
    }
    /* TODO Add support for fragments to set dlm:editAllowed attribute for
     * channel parameters. (2005.11.04 mboyd)
     * 
     * In the commented code below, the check for editAllowed will never be 
     * seen on a parameter element in the 
     * current database schema approach used by DLM. This is because 
     * parameters are second class citizens of the layout structure. They
     * are not found in the up_layout_struct table but only in the 
     * up_layout_param table. DLM functionality like dlm:editAllowed,
     * dlm:moveAllowed, dlm:deleteAllowed, and dlm:addChildAllowed were 
     * implemented without schema changes by adding these as parameters to
     * structural elements and upon loading any parameter that begins with
     * 'dlm:' is placed as an attribute on the containing structural 
     * element. So any channel parameter entry with dlm:editAllowed has that
     * value placed as an attribute on the containing channel not on the 
     * parameter that was meant to have it.
     * 
     * The only solution would be to add special dlm:parm children below
     * channels that would get the editAllowed value and then when creating
     * the DOM don't create those as child elements but use them to set the
     * attribute on the corresponding parameter by having the name of the
     * dlm:parm element be the name of the parameter to which it is to be 
     * related.
     * 
     * The result of this lack of functionality is that fragments can't 
     * mark any channel parameters as dlm:editAllowed='false' thereby
     * further restricting which channel parameters can be edited beyond 
     * what the channel definition specifies during publishing.  
     */
    //Attr editAllowed = targetParm.getAttributeNode( Constants.ATT_EDIT_ALLOWED );
    //if ( editAllowed != null && editAllowed.getNodeValue().equals("false"))
    //    return false;

    // target parm found. See if channel definition will still allow changes.

    Attr override = targetParm.getAttributeNode(Constants.ATT_OVERRIDE);
    if (override != null && !override.getNodeValue().equals(Constants.CAN_OVERRIDE))
        return false;

    // now see if the change is still needed
    if (targetParm.getAttribute(Constants.ATT_VALUE).equals(parmValue))
        return false; // user's edit same as fragment or chan def

    targetParm.setAttribute("value", parmValue);
    return true;
}

From source file:org.apereo.portal.layout.dlm.RDBMDistributedLayoutStore.java

/**
 * Returns the layout for a user. This method overrides the same
 * method in the superclass to return a composite layout for non
 * fragment owners and a regular layout for layout owners. A
 * composite layout is made up of layout pieces from potentially
 * multiple incorporated layouts. If no layouts are defined then
 * the composite layout will be the same as the user's personal
 * layout fragment or PLF, the one holding only those UI elements
 * that they own or incorporated elements that they have been
 * allowed to changed./*www.j ava2s.  com*/
 **/
private DistributedUserLayout _getUserLayout(IPerson person, IUserProfile profile)

{
    final String userName = (String) person.getAttribute("username");
    final FragmentDefinition ownedFragment = this.fragmentUtils.getFragmentDefinitionByOwner(person);
    final boolean isLayoutOwnerDefault = this.isLayoutOwnerDefault(person);
    final Set<String> fragmentNames = new LinkedHashSet<String>();

    final Document ILF;
    final Document PLF = this.getPLF(person, profile);

    // If this user is an owner then ownedFragment will be non null. For
    // fragment owners and owners of any default layout from which a
    // fragment owners layout is copied there should not be any imported
    // distributed layouts. Instead, load their PLF, mark as an owned
    // if a fragment owner, and return.
    if (ownedFragment != null || isLayoutOwnerDefault) {
        ILF = (Document) PLF.cloneNode(true);
        final Element layoutNode = ILF.getDocumentElement();

        final Element ownerDocument = layoutNode.getOwnerDocument().getDocumentElement();
        final NodeList channelNodes = ownerDocument.getElementsByTagName("channel");
        for (int i = 0; i < channelNodes.getLength(); i++) {
            Element channelNode = (Element) channelNodes.item(i);
            final Node chanIdNode = channelNode.getAttributeNode("chanID");
            if (chanIdNode == null || MissingPortletDefinition.CHANNEL_ID.equals(chanIdNode.getNodeValue())) {
                channelNode.getParentNode().removeChild(channelNode);
            }
        }

        if (ownedFragment != null) {
            fragmentNames.add(ownedFragment.getName());
            layoutNode.setAttributeNS(Constants.NS_URI, Constants.ATT_FRAGMENT_NAME, ownedFragment.getName());
            logger.debug("User '{}' is owner of '{}' fragment.", userName, ownedFragment.getName());
        } else if (isLayoutOwnerDefault) {
            layoutNode.setAttributeNS(Constants.NS_URI, Constants.ATT_IS_TEMPLATE_USER, "true");
            layoutNode.setAttributeNS(Constants.NS_URI, Constants.ATT_TEMPLATE_LOGIN_ID,
                    (String) person.getAttribute("username"));
        }
    } else {
        final Locale locale = profile.getLocaleManager().getLocales()[0];
        final List<FragmentDefinition> applicableFragmentDefinitions = this.fragmentUtils
                .getFragmentDefinitionsApplicableToPerson(person);
        final List<Document> applicableLayouts = this.fragmentUtils
                .getFragmentDefinitionUserViewLayouts(applicableFragmentDefinitions, locale);
        final IntegrationResult integrationResult = new IntegrationResult();
        ILF = this.createCompositeILF(person, PLF, applicableLayouts, integrationResult);
        // push optimizations made during merge back into db.
        if (integrationResult.isChangedPLF()) {
            if (logger.isDebugEnabled()) {
                logger.debug("Saving PLF for {} due to changes during merge.",
                        person.getAttribute(IPerson.USERNAME));
            }
            super.setUserLayout(person, profile, PLF, false);
        }
        fragmentNames.addAll(this.fragmentUtils.getFragmentNames(applicableFragmentDefinitions));
    }
    return this.createDistributedUserLayout(person, profile, ILF, fragmentNames);
}

From source file:org.bibsonomy.recommender.tags.simple.MetaInfoTagRecommender.java

/**
 * Parses html file at given url and returns keywords from its meta informations.
 * @param url file's url//from  www  .  j a  va  2s .c o  m
 * @return keywords as given in html file if present, empty string otherwise.
 */
private String getKeywordsForUrl(String url) {
    String keywordsStr = "";
    try {
        final Document document = XmlUtils.getDOM(new URL(url));

        final NodeList metaList = document.getElementsByTagName("meta");
        for (int i = 0; i < metaList.getLength(); i++) {
            final Element metaElement = (Element) metaList.item(i);

            Attr nameAttr = metaElement.getAttributeNode("name");
            if ((nameAttr != null) && (nameAttr.getNodeValue().equalsIgnoreCase("keywords"))) {
                keywordsStr += metaElement.getAttribute("content");
                log.debug("KEYWORDS for URL " + url.toString() + ":" + keywordsStr);
            }
        }
    } catch (IOException ex) {
        // ignore exceptions silently
    }
    return keywordsStr;
}

From source file:org.bibsonomy.scraper.url.kde.ieee.IEEEXploreBookScraper.java

/**
 * @param sc/*  w  w  w . j av a  2 s  .c  o  m*/
 * @return bibtex
 * @throws ScrapingException
 */
public String ieeeBookScrape(ScrapingContext sc) throws ScrapingException {
    try {
        //-- init all NodeLists and Node
        NodeList pres = null;
        Node currNode = null;
        NodeList temp = null;

        //-- init String map for bibtex entries
        String type = IEEE_BOOK;
        String url = sc.getUrl().toString();
        String authors = "";
        String numpages = "";
        String title = "";
        String isbn = "";
        String publisher = "";
        String month = "";
        String year = "";
        String edition = "";
        String abstr = "";

        String bibtexkey = null;
        String _tempabs = null;
        String ident1 = null;
        String ident2 = null;

        //-- get the html doc and parse the DOM
        final Document doc = XmlUtils.getDOM(sc.getPageContent());

        /*
         * -- Search title and extract --
         * The title has always the css-class "headNavBlueXLarge".
         *
         * FIXME: this part could be deprecated. don't knot it at all...
         *
        pres = null;
        pres = doc.getElementsByTagName("span"); //get all <span>-Tags
        for (int i = 0; i < pres.getLength(); i++) {
           Node curr = pres.item(i);
           Element g = (Element)curr;
           Attr own = g.getAttributeNode("class");         
                
           //-- Extract the title
           if ("headNavBlueXLarge".equals(own.getValue())){
              title = curr.getFirstChild().getNodeValue();
           }
        } */

        if (title == null || title.equals("")) {
            ident1 = "<title>";
            ident2 = "</title>";
            if (sc.getPageContent().contains(ident1) && sc.getPageContent().contains(ident2)) {
                int _startIndex = sc.getPageContent().indexOf(ident1) + ident1.length();
                int _endIndex = sc.getPageContent().indexOf(ident2);
                title = sc.getPageContent().substring(_startIndex, _endIndex);
                title = title.replaceAll("IEEEXplore#\\s", "");
            }
        }

        /* 
         * get the abstract block
         * 
         * FIXME: this part could be deprecated. don't knot it at all...
         * 
        ident1 = "<strong>Abstract</strong>";
        ident2 = "<strong>Table of Contents </strong>";
        if (sc.getPageContent().indexOf(ident1) != -1 && sc.getPageContent().indexOf(ident2) != -1 ){
           _tempabs = sc.getPageContent().substring(sc.getPageContent().indexOf(ident1)+ident1.length(),sc.getPageContent().indexOf(ident2)).replaceAll("\\s\\s+", "").replaceAll("(<.+?>)", "").trim();
           abstr = _tempabs;         
        } */

        ident1 = "<span class=\"sectionHeaders\">Abstract</span>";
        ident2 = "<td class=\"bodyCopyGrey\"><p class=\"bodyCopyGreySpaced\"><strong>";
        if (sc.getPageContent().contains(ident1) && sc.getPageContent().contains(ident2)) {
            int _startIndex = sc.getPageContent().indexOf(ident1) + ident1.length();
            int _endIndex = sc.getPageContent().indexOf(ident2);
            _tempabs = sc.getPageContent().substring(_startIndex, _endIndex);
            abstr = _tempabs.replaceAll("\\s\\s+", "").replaceAll("(<.+?>)", "").trim();
        }

        /* 
         * get the book formats like hardcover
         * 
         * FIXME: this part could be deprecated. don't knot it at all...
         * 
         *
        ident1 = "<td class=\"bodyCopyBlackLarge\" nowrap>Hardcover</td>";
        ident2 = "<td class=\"bodyCopyBlackLarge\" nowrap><span class=\"sectionHeaders\">&raquo;</span>";
        if (sc.getPageContent().indexOf(ident1) != -1){
           _format = sc.getPageContent().substring(sc.getPageContent().indexOf(ident1),sc.getPageContent().indexOf(ident2)).replaceAll("\\s\\s+", "").replaceAll("(<.+?>)", "");
                
           _format = _format.substring(_format.indexOf(CONST_ISBN) + CONST_ISBN.length());
           isbn = _format.substring(0,_format.indexOf("&nbsp;"));
        }*/

        /*-- get all <p>-Tags to extract the standard informations
         *  In every standard page the css-class "bodyCopyBlackLargeSpaced"
         *  indicates the collection of all informations.
         * */
        pres = null;
        pres = doc.getElementsByTagName("p"); //get all <p>-Tags
        for (int i = 0; i < pres.getLength(); i++) {
            currNode = pres.item(i);

            if (currNode.hasAttributes()) {
                Element g = (Element) currNode;
                Attr own = g.getAttributeNode("class");
                if ("bodyCopyBlackLargeSpaced".equals(own.getValue()) && currNode.hasChildNodes()) {
                    temp = currNode.getChildNodes();

                    for (int j = 0; j < temp.getLength(); j++) {
                        if (temp.item(j).getNodeValue().indexOf(CONST_DATE) != -1) {
                            String date = temp.item(j).getNodeValue().substring(18);
                            year = date.substring(date.length() - 5).trim();
                            month = date.substring(0, date.length() - 4).trim();
                            // not correct in all cases
                            // publisher = temp.item(j+2).getNodeValue().trim();
                        }
                        if (temp.item(j).getNodeValue().indexOf(CONST_PAGES) != -1) {
                            numpages = temp.item(j).getNodeValue().substring(CONST_PAGES.length()).trim();
                        } else if (temp.item(j).getNodeValue().indexOf(CONST_ON_PAGES) != -1) {
                            numpages = temp.item(j).getNodeValue().substring(CONST_ON_PAGES.length()).trim();
                        }
                        if (temp.item(j).getNodeValue().indexOf(CONST_EDITION) != -1) {
                            edition = temp.item(j).getNodeValue().substring(CONST_EDITION.length()).trim();
                        } else if (temp.item(j).getNodeValue().indexOf(CONST_VOLUME) != -1) {
                            edition = temp.item(j).getNodeValue().substring(CONST_VOLUME.length()).trim();
                        }
                        if (isbn == "" && temp.item(j).getNodeValue().indexOf(CONST_ISBN) != -1) {
                            isbn = temp.item(j).getNodeValue().substring(CONST_ISBN.length()).trim();
                        }
                    }
                    break;
                }
            }
        }

        /*-- Search authors and save them --
         * 
         * FIXME: this part could be deprecated. don't knot it at all...
         * 
        pres = null;
        pres = doc.getElementsByTagName("a"); //get all <a>-Tags
                
        //init vars to count authors to form a bibtex String
        int numaut = 0;
                
         *
         * iterate through the a tags and search the attribute value "<in>aud)" 
         * to identify the authors in the source of the ieeexplore page
         * 
        for (int i = 39; i < pres.getLength(); i++) {
           Node curr = pres.item(i);
           Element g = (Element)curr;
           Attr own = g.getAttributeNode("href");
                
           if (own.getValue().indexOf("<in>au)") != -1){
              //Form Bibtex String by counting authors
              if (numaut > 0 ){
          authors += " and " + curr.getFirstChild().getNodeValue(); 
              }
              if (numaut == 0) {
          numaut=i;
          authors += curr.getFirstChild().getNodeValue();
                
          if (curr.getFirstChild().getNodeValue().indexOf(",") != -1 && bibtexkey == null){
             bibtexkey = curr.getFirstChild().getNodeValue().substring(0,curr.getFirstChild().getNodeValue().trim().indexOf(","));
          } else if (curr.getFirstChild().getNodeValue().trim().indexOf(" ") != -1 && bibtexkey == null){
             bibtexkey = curr.getFirstChild().getNodeValue().trim().substring(0,curr.getFirstChild().getNodeValue().trim().indexOf(" "));
          } else if (bibtexkey == null){
             bibtexkey = curr.getFirstChild().getNodeValue().trim();
          }
              }
           }
        } */

        /*
         * get authors
         */
        if (authors == null || authors.equals("")) {
            ident1 = "<font color=990000><b>";
            ident2 = "<br>";
            int _startIndex = sc.getPageContent().indexOf(ident1) + ident1.length();
            if (sc.getPageContent().contains(ident1)
                    && sc.getPageContent().indexOf(ident2, _startIndex) != -1) {
                int _endIndex = sc.getPageContent().indexOf(ident2, _startIndex);
                authors = sc.getPageContent().substring(_startIndex, _endIndex);
                authors = authors.replaceAll("\\s\\s+", "").replaceAll("(<.+?>)", "").trim();
                authors = authors.replaceAll("&nbsp;&nbsp;", " and ");

                if (authors.endsWith(" and ")) {
                    authors = authors.substring(0, authors.length() - 5);
                }
            }
        }

        //-- kill special chars and add the year to bibtexkey
        if ((isbn == null || !isbn.equals("")) && (year == null || !year.equals(""))) {
            bibtexkey = isbn.replaceAll("-", "");
            bibtexkey = bibtexkey.replaceAll("[^0-9A-Za-z]", "") + ":" + year;
        }

        //create the book-bibtex
        return type + " { " + bibtexkey + ", \n" + "author = {" + authors + "}, \n" + "title = {" + title
                + "}, \n" + "year = {" + year + "}, \n" + "url = {" + url + "}, \n" + "pages = {" + numpages
                + "}, \n" + "edition = {" + edition + "}, \n" + "publisher = {" + publisher + "}, \n"
                + "isbn = {" + isbn + "}, \n" + "abstract = {" + abstr + "}, \n" + "month = {" + month + "}\n}";

    } catch (Exception e) {
        throw new InternalFailureException(e);
    }
}

From source file:org.bibsonomy.scraper.url.kde.ieee.IEEEXploreJournalProceedingsScraper.java

public String ieeeJournalProceedingsScrape(ScrapingContext sc) throws ScrapingException {

    try {// ww w .  j  a  va2 s  . c  om
        //-- init all NodeLists and Node
        NodeList pres = null;
        Node currNode = null;
        NodeList temp = null;

        //-- init Strings for bibtex entries
        // month uncased because of multiple date types
        String type = "";
        String url = sc.getUrl().toString();
        String author = "";
        String year = "";
        String abstr = "";
        String title = "";
        String booktitle = "";
        String volume = null;
        String pages = null;
        String issn = null;
        String isbn = null;
        String doi = null;

        String authors[] = null;
        String tempAuthors = null;

        //-- get the html doc and parse the DOM
        final Document document = XmlUtils.getDOM(sc.getPageContent());

        //get the abstract block
        String ident1 = "<span class=\"sectionHeaders\">Abstract</span><br>";
        String ident2 = "<td class=\"bodyCopyGrey\"><p class=\"bodyCopyGreySpaced\"><strong>Index";
        if (sc.getPageContent().indexOf(ident1) != -1 && sc.getPageContent().indexOf(ident2) != -1) {
            abstr = sc.getPageContent()
                    .substring(sc.getPageContent().indexOf(ident1) + ident1.length(),
                            sc.getPageContent().indexOf(ident2))
                    .replaceAll("\\s\\s+", "").replaceAll("(<.+?>)", "").trim();
        }

        /*-- Get the title of the journal --
         * Iterate through all spans
         */
        pres = null;
        pres = document.getElementsByTagName("span"); //get all <span>-Tags
        for (int i = 0; i < pres.getLength(); i++) {
            Node curr = pres.item(i);
            Element g = (Element) curr;
            Attr own = g.getAttributeNode("class");

            if ("headNavBlueXLarge2".equals(own.getValue())) {
                title = curr.getFirstChild().getNodeValue();
                temp = pres.item(i + 1).getChildNodes();

                if (!"".equals(temp.item(0).getNodeValue())) {
                    tempAuthors = temp.item(0).getNodeValue();

                    if ("\u00A0\u00A0".equals(tempAuthors)) {
                        authors = new String[] { "N/A" };
                    } else {
                        authors = tempAuthors.split("\u00A0\u00A0");
                    }
                }
                break;
            }
        }

        /*-- Get the global infomation like publication date, number of pages ... --
         * iterate through all p's stop at "This paper appears in:" because its
         * available in all journals.
         * Save Nodelist and break the loops.
         * */
        pres = null;
        NodeList match = null;
        pres = document.getElementsByTagName("p"); //get all <p>-Tags
        for (int i = 0; i < pres.getLength(); i++) {
            currNode = pres.item(i);
            temp = currNode.getChildNodes();
            //iterate through childs to find "Publication Date:"
            for (int j = 0; j < temp.getLength(); j++) {
                if (temp.item(j).getNodeValue().indexOf(CONST_BOOKTITLE) != -1) {
                    if (!"".equals(temp.item(1).getFirstChild().getFirstChild().getNodeValue())) {
                        booktitle = temp.item(1).getFirstChild().getFirstChild().getNodeValue();
                    }
                    match = temp;
                    break;
                }
            }
        }
        //get the different childs of the founded p-tag
        for (int i = 0; i < match.getLength(); i++) {
            if (!"".equals(match.item(i).getNodeValue())) {
                String infoMatches = null;
                if (match.item(i).getNodeValue().indexOf(CONST_DATE) != -1) {
                    //extract the year
                    infoMatches = match.item(i).getNodeValue().substring(CONST_DATE.length());
                    StringTokenizer tokenizer = new StringTokenizer(infoMatches);
                    String yearPattern = "\\d{4}";
                    Pattern yearP = Pattern.compile(yearPattern);

                    while (tokenizer.hasMoreTokens()) {
                        String token = tokenizer.nextToken();
                        Matcher matcherYear = yearP.matcher(token);
                        if (matcherYear.matches()) {
                            year = token;
                        }
                    }
                }
                if (volume == null && match.item(i).getNodeValue().indexOf(CONST_VOLUME) != -1) {
                    infoMatches = match.item(i).getNodeValue();
                    volume = infoMatches.substring(infoMatches.indexOf(CONST_VOLUME) + CONST_VOLUME.length(),
                            infoMatches.indexOf(",")).trim();
                }
                if (pages == null && match.item(i).getNodeValue().indexOf(CONST_PAGES) != -1) {
                    infoMatches = match.item(i).getNodeValue();
                    pages = infoMatches.substring(infoMatches.indexOf(CONST_PAGES) + CONST_PAGES.length())
                            .trim();
                }
                if (issn == null)
                    issn = getField(match, i, "ISSN: ");
                if (isbn == null)
                    isbn = getField(match, i, "ISBN: ");
                if (doi == null)
                    doi = getField(match, i, "Digital Object Identifier: ");
            }
        }

        //-- set bibtex type @article for journals & @proceeding for proceedings
        if ((isbn == null || isbn.trim().equals("")) && issn != null && !issn.trim().equals("")) {
            type = IEEE_JOURNAL;
        } else {
            if (title.equals(booktitle)) {
                type = IEEE_PROCEEDINGS;
            } else {
                type = IEEE_INPROCEEDINGS;
            }
        }

        //-- get all authors out of the arraylist and prepare them to bibtex entry "author"
        for (int i = 0; i < authors.length; i++) {
            if (i == authors.length - 1) {
                author += authors[i].trim();
            } else {
                author += authors[i].trim() + " and ";
            }
        }

        //-- kill spaces and add the year to bibtexkey
        //- replace all special chars to avaoid crashes through bibtexkey
        StringBuffer b = new StringBuffer(type + "{" + getName(authors[0]) + ":" + year + ",");
        appendBibtexField(b, "author", author);
        appendBibtexField(b, "abstract", abstr);

        appendBibtexField(b, "title", title);
        appendBibtexField(b, "booktitle", booktitle);
        appendBibtexField(b, "url", url);
        appendBibtexField(b, "year", year);
        appendBibtexField(b, "isbn", isbn);
        appendBibtexField(b, "issn", issn);
        appendBibtexField(b, "doi", doi);
        appendBibtexField(b, "volume", volume);
        appendBibtexField(b, "pages", pages);
        b.append("}");

        return b.toString();

    } catch (Exception e) {
        throw new InternalFailureException(e);
    }
}

From source file:org.bibsonomy.scraper.url.kde.ieee.IEEEXploreStandardsScraper.java

public String ieeeStandardsScrape(ScrapingContext sc) throws ScrapingException {
    try {/*from www  . j  a  v  a  2  s  .co  m*/
        //-- init all NodeLists and Node
        NodeList pres = null;
        Node currNode = null;
        NodeList temp = null;

        //-- init String map for bibtex entries
        String type = IEEE_STANDARDS;
        String url = sc.getUrl().toString();
        String numpages = "";
        String title = "";
        String isbn = "";
        String abstr = "";
        String year = "";

        //-- get the html doc and parse the DOM
        final Document document = XmlUtils.getDOM(sc.getPageContent());

        /* -- get the spans to extract the title and abstract
         */
        pres = null;
        pres = document.getElementsByTagName("span"); //get all <span>-Tags
        for (int i = 0; i < pres.getLength(); i++) {
            currNode = pres.item(i);
            if (currNode.hasAttributes()) {
                Element g = (Element) currNode;
                Attr own = g.getAttributeNode("class");
                //-- Extract the title
                if ("headNavBlueXLarge2".equals(own.getValue())) {
                    temp = currNode.getChildNodes();
                    title = temp.item(temp.getLength() - 1).getNodeValue().trim();
                }
                //-- Extract the abstract
                if ("sectionHeaders".equals(own.getValue())
                        && "Abstract".equals(currNode.getFirstChild().getNodeValue())) {
                    abstr = currNode.getParentNode().getLastChild().getNodeValue().trim();
                }
            }
        }

        /*-- get all <p>-Tags to extract the standard informations
         *  In every standard page the css-class "bodyCopyBlackLargeSpaced"
         *  indicates the collection of all informations.
         * */
        pres = null;
        pres = document.getElementsByTagName("p"); //get all <p>-Tags
        for (int i = 0; i < pres.getLength(); i++) {
            currNode = pres.item(i);
            if (currNode.hasAttributes()) {
                Element g = (Element) currNode;
                Attr own = g.getAttributeNode("class");
                if ("bodyCopyBlackLargeSpaced".equals(own.getValue())) {
                    temp = currNode.getChildNodes();

                    for (int j = 0; j < temp.getLength(); j++) {
                        if (temp.item(j).getNodeValue().indexOf(CONST_DATE) != -1) {
                            String date = temp.item(j).getNodeValue().substring(CONST_DATE.length()).trim();
                            year = date.substring(date.length() - 4).trim();
                        }
                        if (temp.item(j).getNodeValue().indexOf(CONST_PAGE) != -1) {
                            numpages = temp.item(j).getNodeValue().substring(CONST_PAGE.length()).trim();
                        }
                        if (temp.item(j).getNodeValue().indexOf(CONST_EISBN) != -1) {
                            isbn = temp.item(j).getNodeValue().substring(CONST_EISBN.length()).trim();
                        }
                    }
                }
            }
        }

        //create valid bibtex snippet
        return type + " {," + "title = {" + title + "}, " + "year = {" + year + "}, " + "url = {" + url + "}, "
                + "pages = {" + numpages + "}, " + "abstract = {" + abstr + "}, " + "isbn = {" + isbn + "}}";

    } catch (Exception e) {
        throw new InternalFailureException(e);
    }
}

From source file:org.dita.dost.AbstractIntegrationTest.java

private Document rewriteIds(final Document doc, final Map<String, Pattern> patterns) {
    final Map<String, String> idMap = new HashMap<>();
    AtomicInteger counter = new AtomicInteger();
    final NodeList ns = doc.getElementsByTagName("*");
    for (int i = 0; i < ns.getLength(); i++) {
        final Element e = (Element) ns.item(i);
        for (Map.Entry<String, Pattern> p : patterns.entrySet()) {
            final Attr id = e.getAttributeNode(p.getKey());
            if (id != null) {
                if (p.getKey().equals("headers")) {// split value
                    final List<String> res = new ArrayList<>();
                    for (final String v : id.getValue().trim().split("\\s+")) {
                        rewriteId(v, idMap, counter, p.getValue());
                        res.add(idMap.getOrDefault(v, v));
                    }//  www  . j av  a2 s . co m
                    id.setNodeValue(res.stream().collect(Collectors.joining(" ")));

                } else {
                    final String v = id.getValue();
                    rewriteId(v, idMap, counter, p.getValue());
                    if (idMap.containsKey(v)) {
                        id.setNodeValue(idMap.get(v));
                    }
                }
            }
        }
    }
    return doc;
}

From source file:org.dita.dost.module.BranchFilterModule.java

/** Rewrite href or copy-to if duplicates exist. */
private void rewriteDuplicates(final Element root) {
    // collect href and copy-to
    final Map<URI, Map<Set<URI>, List<Attr>>> refs = new HashMap<>();
    for (final Element e : getTopicrefs(root)) {
        Attr attr = e.getAttributeNode(BRANCH_COPY_TO);
        if (attr == null) {
            attr = e.getAttributeNode(ATTRIBUTE_NAME_COPY_TO);
            if (attr == null) {
                attr = e.getAttributeNode(ATTRIBUTE_NAME_HREF);
            }//w  w  w . j  ava 2 s. co m
        }
        if (attr != null) {
            final URI h = stripFragment(map.resolve(attr.getValue()));
            Map<Set<URI>, List<Attr>> attrsMap = refs.computeIfAbsent(h, k -> new HashMap<>());
            final Set<URI> currentFilter = getBranchFilters(e);
            List<Attr> attrs = attrsMap.computeIfAbsent(currentFilter, k -> new ArrayList<>());
            attrs.add(attr);
        }
    }
    // check and rewrite
    for (final Map.Entry<URI, Map<Set<URI>, List<Attr>>> ref : refs.entrySet()) {
        final Map<Set<URI>, List<Attr>> attrsMaps = ref.getValue();
        if (attrsMaps.size() > 1) {
            if (attrsMaps.containsKey(Collections.EMPTY_LIST)) {
                attrsMaps.remove(Collections.EMPTY_LIST);
            } else {
                Set<URI> first = attrsMaps.keySet().iterator().next();
                attrsMaps.remove(first);
            }
            int i = 1;
            for (final Map.Entry<Set<URI>, List<Attr>> attrsMap : attrsMaps.entrySet()) {
                final String suffix = "-" + i;
                final List<Attr> attrs = attrsMap.getValue();
                for (final Attr attr : attrs) {
                    final String gen = addSuffix(attr.getValue(), suffix);
                    logger.info(MessageUtils.getMessage("DOTJ065I", attr.getValue(), gen)
                            .setLocation(attr.getOwnerElement()).toString());
                    if (attr.getName().equals(BRANCH_COPY_TO)) {
                        attr.setValue(gen);
                    } else {
                        attr.getOwnerElement().setAttribute(BRANCH_COPY_TO, gen);
                    }

                    final URI dstUri = map.resolve(gen);
                    if (dstUri != null) {
                        final FileInfo hrefFileInfo = job.getFileInfo(currentFile.resolve(attr.getValue()));
                        if (hrefFileInfo != null) {
                            final URI newResult = addSuffix(hrefFileInfo.result, suffix);
                            final FileInfo.Builder dstBuilder = new FileInfo.Builder(hrefFileInfo).uri(dstUri)
                                    .result(newResult);
                            if (hrefFileInfo.format == null) {
                                dstBuilder.format(ATTR_FORMAT_VALUE_DITA);
                            }
                            final FileInfo dstFileInfo = dstBuilder.build();
                            job.add(dstFileInfo);
                        }
                    }
                }
                i++;
            }
        }
    }
}

From source file:org.dita.dost.module.BranchFilterModule.java

/** Get all topicrefs */
private List<Element> getTopicrefs(final Element root) {
    final List<Element> res = new ArrayList<>();
    final NodeList all = root.getElementsByTagName("*");
    for (int i = 0; i < all.getLength(); i++) {
        final Element elem = (Element) all.item(i);
        if (MAP_TOPICREF.matches(elem) && isDitaFormat(elem.getAttributeNode(ATTRIBUTE_NAME_FORMAT))
                && !elem.getAttribute(ATTRIBUTE_NAME_SCOPE).equals(ATTR_SCOPE_VALUE_EXTERNAL)) {
            res.add(elem);/* w  ww.  jav a2  s .co  m*/
        }
    }
    return res;
}