Пример #1
0
  /**
   * Return the iso language used in most paragaph styles (in a well-structured document this will
   * be the default language) TODO: Base on content rather than style
   *
   * @return the iso language
   */
  public String getMajorityLanguage() {
    Hashtable<Object, Integer> langs = new Hashtable<Object, Integer>();

    // Read the default language from the default paragraph style
    String sDefaultLang = null;
    StyleWithProperties style = getDefaultParStyle();
    if (style != null) {
      sDefaultLang = style.getProperty(XMLString.FO_LANGUAGE);
    }

    // Collect languages from paragraph styles
    Enumeration<Object> enumeration = getParStyles().getStylesEnumeration();
    while (enumeration.hasMoreElements()) {
      style = (StyleWithProperties) enumeration.nextElement();
      String sLang = style.getProperty(XMLString.FO_LANGUAGE);
      if (sLang == null) {
        sLang = sDefaultLang;
      }
      if (sLang != null) {
        int nCount = 1;
        if (langs.containsKey(sLang)) {
          nCount = langs.get(sLang).intValue() + 1;
        }
        langs.put(sLang, new Integer(nCount));
      }
    }

    // Find the most common language
    int nMaxCount = 0;
    String sMajorityLanguage = null;
    enumeration = langs.keys();
    while (enumeration.hasMoreElements()) {
      String sLang = (String) enumeration.nextElement();
      int nCount = langs.get(sLang).intValue();
      if (nCount > nMaxCount) {
        nMaxCount = nCount;
        sMajorityLanguage = sLang;
      }
    }
    return sMajorityLanguage;
  }
Пример #2
0
 private void loadStylesFromDOM(Node node, boolean bAllParagraphsAreSoft) {
   // node should be office:master-styles, office:styles or office:automatic-styles
   boolean bAutomatic = XMLString.OFFICE_AUTOMATIC_STYLES.equals(node.getNodeName());
   if (node.hasChildNodes()) {
     NodeList nl = node.getChildNodes();
     int nLen = nl.getLength();
     for (int i = 0; i < nLen; i++) {
       Node child = nl.item(i);
       if (child.getNodeType() == Node.ELEMENT_NODE) {
         if (child.getNodeName().equals(XMLString.STYLE_STYLE)) {
           String sFamily = Misc.getAttribute(child, XMLString.STYLE_FAMILY);
           if ("text".equals(sFamily)) {
             text.loadStyleFromDOM(child, bAutomatic);
           } else if ("paragraph".equals(sFamily)) {
             par.loadStyleFromDOM(child, bAutomatic && !bAllParagraphsAreSoft);
           } else if ("section".equals(sFamily)) {
             section.loadStyleFromDOM(child, bAutomatic);
           } else if ("table".equals(sFamily)) {
             table.loadStyleFromDOM(child, bAutomatic);
           } else if ("table-column".equals(sFamily)) {
             column.loadStyleFromDOM(child, bAutomatic);
           } else if ("table-row".equals(sFamily)) {
             row.loadStyleFromDOM(child, bAutomatic);
           } else if ("table-cell".equals(sFamily)) {
             cell.loadStyleFromDOM(child, bAutomatic);
           } else if ("graphics".equals(sFamily)) {
             frame.loadStyleFromDOM(child, bAutomatic);
           } else if ("graphic".equals(sFamily)) { // oasis
             frame.loadStyleFromDOM(child, bAutomatic);
           } else if ("presentation".equals(sFamily)) {
             presentation.loadStyleFromDOM(child, bAutomatic);
           } else if ("drawing-page".equals(sFamily)) {
             // Bug in OOo 1.x: The same name may be used for a real and an automatic style...
             if (drawingPage.getStyle(Misc.getAttribute(child, XMLString.STYLE_NAME)) == null) {
               drawingPage.loadStyleFromDOM(child, bAutomatic);
             }
           }
         } else if (child.getNodeName().equals(XMLString.STYLE_PAGE_MASTER)) { // old
           pageLayout.loadStyleFromDOM(child, bAutomatic);
         } else if (child.getNodeName().equals(XMLString.STYLE_PAGE_LAYOUT)) { // oasis
           pageLayout.loadStyleFromDOM(child, bAutomatic);
         } else if (child.getNodeName().equals(XMLString.STYLE_MASTER_PAGE)) {
           masterPage.loadStyleFromDOM(child, bAutomatic);
           if (firstMasterPage == null) {
             firstMasterPage =
                 (MasterPage) masterPage.getStyle(Misc.getAttribute(child, XMLString.STYLE_NAME));
           }
         } else if (child.getNodeName().equals(XMLString.TEXT_LIST_STYLE)) {
           list.loadStyleFromDOM(child, bAutomatic);
         } else if (child.getNodeName().equals(XMLString.TEXT_OUTLINE_STYLE)) {
           outline.loadStyleFromDOM(child);
         } else if (child.getNodeName().equals(XMLString.STYLE_DEFAULT_STYLE)) {
           String sFamily = Misc.getAttribute(child, XMLString.STYLE_FAMILY);
           if ("paragraph".equals(sFamily)) {
             StyleWithProperties defaultPar = new StyleWithProperties();
             defaultPar.loadStyleFromDOM(child);
             par.setDefaultStyle(defaultPar);
           } else if ("graphics".equals(sFamily) || "graphic".equals(sFamily)) { // oasis: no s
             StyleWithProperties defaultFrame = new StyleWithProperties();
             defaultFrame.loadStyleFromDOM(child);
             frame.setDefaultStyle(defaultFrame);
           } else if ("table-cell".equals(sFamily)) {
             StyleWithProperties defaultCell = new StyleWithProperties();
             defaultCell.loadStyleFromDOM(child);
             cell.setDefaultStyle(defaultCell);
           }
         }
       }
     }
   }
 }
Пример #3
0
  private void traverseContent(Element node) {
    // Handle this node first
    String sName = node.getTagName();
    if (sName.equals(XMLString.TEXT_P)) {
      // collectMasterPage(getParStyle(node.getAttribute(XMLString.TEXT_STYLE_NAME)));
    } else if (sName.equals(XMLString.TEXT_H)) {
      int nLevel;
      if (node.hasAttribute(XMLString.TEXT_OUTLINE_LEVEL)) {
        nLevel = Misc.getPosInteger(node.getAttribute(XMLString.TEXT_OUTLINE_LEVEL), 1);
      } else {
        nLevel = Misc.getPosInteger(node.getAttribute(XMLString.TEXT_LEVEL), 1);
      }
      StyleWithProperties style = getParStyle(node.getAttribute(XMLString.TEXT_STYLE_NAME));
      // collectMasterPage(style);
      if (1 <= nLevel && nLevel <= 10 && heading[nLevel] == null) {
        if (style != null && style.isAutomatic()) {
          heading[nLevel] = getParStyle(style.getParentName());
        } else {
          heading[nLevel] = style;
        }
      }
    } else if (sName.equals(XMLString.TEXT_SEQUENCE)) {
      String sSeqName = Misc.getAttribute(node, XMLString.TEXT_NAME);
      String sRefName = Misc.getAttribute(node, XMLString.TEXT_REF_NAME);
      if (sSeqName != null) {
        Element par = getParagraph(node);
        if (!sequenceNames.containsKey(par)) {
          // Only the first text:seqence should be registered as possible caption sequence
          sequenceNames.put(par, sSeqName);
        }
        if (sRefName != null) {
          seqrefNames.put(sRefName, sSeqName);
        }
      }
    } else if (sName.equals(XMLString.TEXT_FOOTNOTE_REF)) {
      collectRefName(footnoteRef, node);
    } else if (sName.equals(XMLString.TEXT_ENDNOTE_REF)) {
      collectRefName(endnoteRef, node);
    } else if (sName.equals(XMLString.TEXT_NOTE_REF)) { // oasis
      String sClass = Misc.getAttribute(node, XMLString.TEXT_NOTE_CLASS);
      if ("footnote".equals(sClass)) {
        collectRefName(footnoteRef, node);
      } else if ("endnote".equals(sClass)) {
        collectRefName(endnoteRef, node);
      }
    } else if (sName.equals(XMLString.TEXT_REFERENCE_MARK)) {
      collectMarkInHeading(referenceHeading, node);
    } else if (sName.equals(XMLString.TEXT_REFERENCE_MARK_START)) {
      collectMarkInHeading(referenceHeading, node);
    } else if (sName.equals(XMLString.TEXT_REFERENCE_REF)) {
      collectRefName(referenceRef, node);
    } else if (sName.equals(XMLString.TEXT_BOOKMARK)) {
      collectMarkInHeading(bookmarkHeading, node);
    } else if (sName.equals(XMLString.TEXT_BOOKMARK_START)) {
      collectMarkInHeading(bookmarkHeading, node);
    } else if (sName.equals(XMLString.TEXT_BOOKMARK_REF)) {
      collectRefName(bookmarkRef, node);
    } else if (sName.equals(XMLString.TEXT_SEQUENCE_REF)) {
      collectRefName(sequenceRef, node);
    } else if (sName.equals(XMLString.TEXT_A)) {
      String sHref = node.getAttribute(XMLString.XLINK_HREF);
      if (sHref != null && sHref.startsWith("#")) {
        links.add(sHref.substring(1));
      }
    } else if (sName.equals(XMLString.OFFICE_FORMS)) {
      forms.read(node);
    } else if (sName.equals(XMLString.TEXT_TABLE_OF_CONTENT)) {
      TocReader tocReader = new TocReader(node);
      indexes.put(node, tocReader);
      indexSourceStyles.addAll(tocReader.getIndexSourceStyles());
    } else if (sName.equals(XMLString.TEXT_TABLE_INDEX)
        || sName.equals(XMLString.TEXT_ILLUSTRATION_INDEX)) {
      LoftReader loftReader = new LoftReader(node);
      indexes.put(node, loftReader);
      if (loftReader.useCaption()) {
        if (loftReader.isTableIndex()) {
          tableSequenceNames.add(loftReader.getCaptionSequenceName());
        } else {
          figureSequenceNames.add(loftReader.getCaptionSequenceName());
        }
      }
    }
    // todo: other indexes

    // Traverse the children
    Node child = node.getFirstChild();
    while (child != null) {
      if (child.getNodeType() == Node.ELEMENT_NODE) {
        traverseContent((Element) child);
      }
      child = child.getNextSibling();
    }

    // Collect automatic captions sequences
    // Use OOo defaults: Captions have style names Illustration and Table resp.
    if ((sAutoFigureSequenceName == null || sAutoTableSequenceName == null)
        && sName.equals(XMLString.TEXT_P)) {
      String sStyleName =
          getParStyles().getDisplayName(node.getAttribute(XMLString.TEXT_STYLE_NAME));
      if (sAutoFigureSequenceName == null) {
        if ("Illustration".equals(sStyleName)) {
          sAutoFigureSequenceName = getSequenceName(node);
        }
      }
      if (sAutoTableSequenceName == null) {
        if ("Table".equals(sStyleName)) {
          sAutoTableSequenceName = getSequenceName(node);
        }
      }
    }
  }