Package org.odftoolkit.odfdom.pkg

Examples of org.odftoolkit.odfdom.pkg.OdfElement


      //2.2. get the reference of each style which occurred in the current page
      for (int i = 0; i < stylesList.getLength(); i++) {
        Node item = stylesList.item(i);
        if (item instanceof OdfElement) {
          OdfElement node = (OdfElement) item;
          String styleName = node.getAttributeNS(OdfDocumentNamespace.STYLE.getUri(), "name");
          if (styleName != null) {
            //search the styleName contained at the current page element
            NodeList styleNodes = (NodeList) xpath.evaluate("//*[@*='" + styleName + "']", contentDom, XPathConstants.NODESET);
            int styleCnt = styleNodes.getLength();
            if (styleCnt > 1) {
              //the first styleName is occurred in the style definition
              //so check if the second styleName and last styleName is occurred in the current page element
              //if yes, then remove it
              OdfElement elementFirst = (OdfElement) styleNodes.item(1);
              OdfElement elementLast = (OdfElement) styleNodes.item(styleCnt - 1);
              boolean isSamePage = false;
              if (elementFirst instanceof DrawPageElement) {
                DrawPageElement tempPage = (DrawPageElement) elementFirst;
                if (tempPage.equals(slideEle)) {
                  isSamePage = true;
View Full Code Here


    try {
      OdfContentDom contentDom = getContentDom();
      XPath xpath = contentDom.getXPath();
      NodeList linkNodes = (NodeList) xpath.evaluate("//*[@xlink:href]", contentDom, XPathConstants.NODESET);
      for (int i = 0; i < linkNodes.getLength(); i++) {
        OdfElement object = (OdfElement) linkNodes.item(i);
        String refObjPath = object.getAttributeNS(OdfDocumentNamespace.XLINK.getUri(), "href");
        int relation = slideEle.compareDocumentPosition(object);
        //if slide element contains the returned element which has the xlink:href reference
        if ((relation & Node.DOCUMENT_POSITION_CONTAINED_BY) > 0 && refObjPath != null && refObjPath.length() > 0) {
          //the path of the object is start with "./"
          NodeList pathNodes = (NodeList) xpath.evaluate("//*[@xlink:href='" + refObjPath + "']", getContentDom(), XPathConstants.NODESET);
View Full Code Here

      OdfPackageDocument srcDoc = fileDom.getDocument();
      //new a map to put the original name and the rename string, in case that the same name might be referred by the slide several times.
      HashMap<String, String> objectRenameMap = new HashMap<String, String>();
      NodeList linkNodes = (NodeList) xpath.evaluate(".//*[@xlink:href]", sourceCloneEle, XPathConstants.NODESET);
      for (int i = 0; i <= linkNodes.getLength(); i++) {
        OdfElement object = null;
        if (linkNodes.getLength() == i) {
          if (sourceCloneEle.hasAttributeNS(OdfDocumentNamespace.XLINK.getUri(), "href")) {
            object = sourceCloneEle;
          } else {
            break;
          }
        } else {
          object = (OdfElement) linkNodes.item(i);
        }
        String refObjPath = object.getAttributeNS(OdfDocumentNamespace.XLINK.getUri(), "href");
        if (refObjPath != null && refObjPath.length() > 0) {
          //the path of the object is start with "./"
          boolean hasPrefix = false;
          String prefix = "./";
          if (refObjPath.startsWith(prefix)) {
            refObjPath = refObjPath.substring(2);
            hasPrefix = true;
          }
          //check if the current document contains the same path
          OdfFileEntry fileEntry = getPackage().getFileEntry(refObjPath);
          //note: if refObjPath is a directory, it must end with '/'
          if (fileEntry == null) {
            fileEntry = getPackage().getFileEntry(refObjPath + "/");
          }
          String newObjPath = refObjPath;
          if (fileEntry != null) {
            //rename the object path
            newObjPath = objectRenameMap.get(refObjPath);
            if (newObjPath == null) {
              //if refObjPath still contains ".", it means that it has the suffix
              //then change the name before the suffix string
              int dotIndex = refObjPath.indexOf(".");
              if (dotIndex != -1) {
                newObjPath = refObjPath.substring(0, dotIndex) + "-" + makeUniqueName() + refObjPath.substring(dotIndex);
              } else {
                newObjPath = refObjPath + "-" + makeUniqueName();
              }
              objectRenameMap.put(refObjPath, newObjPath);
            }
            object.setAttributeNS(OdfDocumentNamespace.XLINK.getUri(), "xlink:href", hasPrefix ? (prefix + newObjPath) : newObjPath);
          }
          InputStream is = srcDoc.getPackage().getInputStream(refObjPath);
          if (is != null) {
            String mediaType = srcDoc.getPackage().getFileEntry(refObjPath).getMediaTypeString();
            getPackage().insert(is, newObjPath, mediaType);
View Full Code Here

      srcStyleDefNodeList = (NodeList) xpath.evaluate("//*[@" + styleQName + "]", doc.getStylesDom(), XPathConstants.NODESET);
      HashMap<OdfElement, List<OdfElement>> srcDrawStyleCloneEleList = new HashMap<OdfElement, List<OdfElement>>();
      HashMap<OdfElement, OdfElement> appendDrawStyleList = new HashMap<OdfElement, OdfElement>();
      Iterator<OdfElement> iter = appendContentStyleList.keySet().iterator();
      while (iter.hasNext()) {
        OdfElement styleElement = iter.next();
        OdfElement cloneStyleElement = appendContentStyleList.get(styleElement);
        getCopyStyleList(styleElement, cloneStyleElement, styleQName, srcStyleDefNodeList, srcDrawStyleCloneEleList, appendDrawStyleList, false);
      }
      iter = appendStylesStyleList.keySet().iterator();
      while (iter.hasNext()) {
        OdfElement styleElement = iter.next();
        OdfElement cloneStyleElement = appendStylesStyleList.get(styleElement);
        getCopyStyleList(styleElement, cloneStyleElement, styleQName, srcStyleDefNodeList, srcDrawStyleCloneEleList, appendDrawStyleList, false);
      }
      //2.2 rename, copy the referred style element to the corresponding position in the dom tree
      //note: "draw:name" style element only exist in styles.dom
      insertCollectedStyle(styleQName, srcDrawStyleCloneEleList, getStylesDom(), appendDrawStyleList);
View Full Code Here

      NodeList destStyleNodeList = (NodeList) xpath.evaluate("//*[@" + styleQName + "]", dom, XPathConstants.NODESET);

//      HashMap<String, String> styleRenameMap = new HashMap<String, String>();
      Iterator<OdfElement> iter = srcStyleCloneEleList.keySet().iterator();
      while (iter.hasNext()) {
        OdfElement styleElement = iter.next();
        OdfElement cloneStyleElement = appendStyleList.get(styleElement);
        if (cloneStyleElement == null) {
          cloneStyleElement = (OdfElement) styleElement.cloneNode(true);
          appendStyleList.put(styleElement, cloneStyleElement);
        }
        String styleName = styleElement.getAttributeNS(styleURI, styleLocalName);
        List<String> newStyleNameList = styleRenameMap.get(styleName);
        // if the newStyleNameList != null, means that styleName exists in dest document
        // and it has already been renamed
        if ((newStyleNameList != null)
            || (isStyleNameExist(destStyleNodeList, styleName) != null)) {
          String newStyleName = null;
          if (newStyleNameList == null) {
            newStyleNameList = new ArrayList<String>();
            newStyleName = styleName + "-" + makeUniqueName();
            newStyleNameList.add(newStyleName);
            styleRenameMap.put(styleName, newStyleNameList);
          } else {
            for (int i = 0; i < newStyleNameList.size(); i++) {
              String styleNameIter = newStyleNameList.get(i);
              OdfElement destStyleElementWithNewName = isStyleNameExist(destStyleNodeList, styleNameIter);
              //check if the two style elements have the same content
              //if not, the cloneStyleElement should rename, rather than reuse the new style name
              cloneStyleElement.setAttributeNS(styleURI, styleQName, styleNameIter);
              if ((destStyleElementWithNewName != null) && destStyleElementWithNewName.equals(cloneStyleElement)) {
                newStyleName = styleNameIter;
                break;
              }
            }
            if (newStyleName == null) {
              newStyleName = styleName + "-" + makeUniqueName();
              newStyleNameList.add(newStyleName);
            }
          }
          // if newStyleName has been set in the element as the new name
          // which means that the newStyleName is conform to the odf spec
          // then change element style reference name
          if (changeStyleRefName(srcStyleCloneEleList.get(styleElement), styleName, newStyleName)) {
            cloneStyleElement.setAttributeNS(styleURI, styleQName, newStyleName);
            //if display name should also be renamed
            String displayName = cloneStyleElement.getAttributeNS(styleURI, "display-name");
            if ((displayName != null) && (displayName.length() > 0)) {
              cloneStyleElement.setAttributeNS(styleURI, stylePrefix + ":display-name",
                  displayName + newStyleName.substring(newStyleName.length() - 8));
            }
          }

        }
      }

      iter = appendStyleList.keySet().iterator();
      while (iter.hasNext()) {
        OdfElement styleElement = iter.next();
        OdfElement cloneStyleElement = appendStyleList.get(styleElement);
        String newStyleName = cloneStyleElement.getAttributeNS(styleURI, styleLocalName);
        Boolean isAppended = styleAppendMap.get(newStyleName);
        //if styleAppendMap contain the newStyleName,
        //means that cloneStyleElement has already been appended
        if ((isAppended != null) && isAppended.booleanValue() == true) {
          continue;
        } else {
          styleAppendMap.put(newStyleName, true);
        }
        OdfElement cloneForeignStyleElement = (OdfElement) cloneForeignElement(cloneStyleElement, dom, true);
        String styleElePath = getElementPath(styleElement);
        appendForeignStyleElement(cloneForeignStyleElement, dom, styleElePath);
        copyForeignLinkRef(cloneStyleElement);
      }
    } catch (Exception e) {
View Full Code Here

      String styleLocalName = OdfNamespace.getLocalPart(styleQName);
      String styleURI = OdfDocumentNamespace.STYLE.getUri();
      //OdfElement override the "toString" method
      String cloneEleStr = cloneEle.toString();
      for (int i = 0; i < srcStyleNodeList.getLength(); i++) {
        OdfElement styleElement = (OdfElement) srcStyleNodeList.item(i);
        String styleName = styleElement.getAttributeNS(styleURI, styleLocalName);
        if (styleName != null) {
          int index = 0;
          index = cloneEleStr.indexOf("=\"" + styleName + "\"", index);
          while (index >= 0) {
            String subStr = cloneEleStr.substring(0, index);
            int lastSpaceIndex = subStr.lastIndexOf(' ');
            String attrStr = subStr.substring(lastSpaceIndex + 1, index);
            XPath xpath = ((OdfFileDom) cloneEle.getOwnerDocument()).getXPath();
            NodeList styleRefNodes = (NodeList) xpath.evaluate(".//*[@" + attrStr + "='" + styleName + "']", cloneEle, XPathConstants.NODESET);
            boolean isExist = false;
            for (int j = 0; j <= styleRefNodes.getLength(); j++) {
              OdfElement styleRefElement = null;
              if (j == styleRefNodes.getLength()) {
                isExist = isStyleNameRefExist(cloneEle, styleName, false);
                if (isExist) {
                  styleRefElement = cloneEle;
                } else {
                  continue;
                }
              } else {
                OdfElement tmpElement = (OdfElement) styleRefNodes.item(j);
                if (isStyleNameRefExist(tmpElement, styleName, false)) {
                  styleRefElement = tmpElement;
                } else {
                  continue;
                }
              }
              boolean hasLoopStyleDef = true;
              if (copyStyleEleList.get(styleElement) == null) {
                List<OdfElement> styleRefEleList = new ArrayList<OdfElement>();
                copyStyleEleList.put(styleElement, styleRefEleList);
                hasLoopStyleDef = false;
              }
              copyStyleEleList.get(styleElement).add(styleRefElement);

              OdfElement cloneStyleElement = appendStyleList.get(styleElement);
              if (cloneStyleElement == null) {
                cloneStyleElement = (OdfElement) styleElement.cloneNode(true);
                appendStyleList.put(styleElement, cloneStyleElement);
              }
              if (loop && !hasLoopStyleDef) {
View Full Code Here

        //should new the element since the current path node
        if (isExist) {
          isExist = false;
        }
        StringTokenizer token2 = new StringTokenizer(onePath, "@");
        OdfElement newElement = dom.createElementNS(OdfName.newName(token2.nextToken(), token2.nextToken()));
        parentNode.appendChild(newElement);
        parentNode = newElement;
      }
    }
    parentNode.appendChild(cloneStyleEle);
View Full Code Here

  //if true then set newStyleName attribute value successfully
  //if false means that the newStyleName value is not conform to the ODF spec, so do not modify the oldStyleName
  private boolean changeStyleRefName(List<OdfElement> list, String oldStyleName, String newStyleName) {
    boolean rtn = false;
    for (int index = 0; index < list.size(); index++) {
      OdfElement element = list.get(index);
      NamedNodeMap attributes = element.getAttributes();

      if (attributes != null) {
        for (int i = 0; i < attributes.getLength(); i++) {
          Node item = attributes.item(i);
          String value = item.getNodeValue();
View Full Code Here

  //check if nodeList contains the node that "style:name" attribute has the same value with styleName
  //Note: nodeList here is all the style definition list
  private OdfElement isStyleNameExist(NodeList nodeList,
      String styleName) {
    for (int i = 0; i < nodeList.getLength(); i++) {
      OdfElement element = (OdfElement) nodeList.item(i);
      String name = element.getAttributeNS(OdfDocumentNamespace.STYLE.getUri(), "name");
      if (name.equals(styleName)) //return true;
      {
        return element;
      }
    }
View Full Code Here

   * @return  Returns a duplicated element which is not in the DOM tree with the specified element
   */
  public Node cloneForeignElement(Node element, OdfFileDom dom, boolean deep) {
    checkAllSlideName();
    if (element instanceof OdfElement) {
      OdfElement cloneElement = dom.createElementNS(((OdfElement) element).getOdfName());

      NamedNodeMap attributes = element.getAttributes();
      if (attributes != null) {
        for (int i = 0; i < attributes.getLength(); i++) {
          Node item = attributes.item(i);
          String qname = null;
          String prefix = item.getPrefix();
          if (prefix == null) {
            qname = item.getLocalName();
          } else {
            qname = prefix + ":" + item.getLocalName();
          }

          cloneElement.setAttributeNS(item.getNamespaceURI(), qname, item.getNodeValue());
        }
      }

      if (deep) {
        Node childNode = element.getFirstChild();
        while (childNode != null) {
          cloneElement.appendChild(cloneForeignElement(childNode, dom, true));
          childNode = childNode.getNextSibling();
        }
      }

      return cloneElement;
View Full Code Here

TOP

Related Classes of org.odftoolkit.odfdom.pkg.OdfElement

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.