You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

XWPFHyperlinkDecorator.java 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xwpf.model;
  16. import org.apache.poi.xwpf.usermodel.XWPFHyperlinkRun;
  17. import org.apache.poi.xwpf.usermodel.XWPFParagraph;
  18. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHyperlink;
  19. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
  20. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
  21. /**
  22. * Decorator class for XWPFParagraph allowing to add hyperlinks
  23. * found in paragraph to its text.
  24. *
  25. * Note - adds the hyperlink at the end, not in the right place...
  26. *
  27. * @deprecated Use {@link XWPFHyperlinkRun} instead
  28. */
  29. @Deprecated
  30. public class XWPFHyperlinkDecorator extends XWPFParagraphDecorator {
  31. private StringBuffer hyperlinkText;
  32. /**
  33. * @param nextDecorator The next decorator to use
  34. * @param outputHyperlinkUrls Should we output the links too, or just the link text?
  35. */
  36. public XWPFHyperlinkDecorator(XWPFParagraphDecorator nextDecorator, boolean outputHyperlinkUrls) {
  37. this(nextDecorator.paragraph, nextDecorator, outputHyperlinkUrls);
  38. }
  39. /**
  40. * @param prgrph The paragraph of text to work on
  41. * @param outputHyperlinkUrls Should we output the links too, or just the link text?
  42. */
  43. public XWPFHyperlinkDecorator(XWPFParagraph prgrph, XWPFParagraphDecorator nextDecorator, boolean outputHyperlinkUrls) {
  44. super(prgrph, nextDecorator);
  45. hyperlinkText = new StringBuffer();
  46. // loop over hyperlink anchors
  47. for(CTHyperlink link : paragraph.getCTP().getHyperlinkArray()){
  48. for (CTR r : link.getRArray()) {
  49. // Loop over text runs
  50. for (CTText text : r.getTArray()){
  51. hyperlinkText.append(text.getStringValue());
  52. }
  53. }
  54. if(outputHyperlinkUrls && paragraph.getDocument().getHyperlinkByID(link.getId()) != null) {
  55. hyperlinkText.append(" <"+paragraph.getDocument().getHyperlinkByID(link.getId()).getURL()+">");
  56. }
  57. }
  58. }
  59. public String getText()
  60. {
  61. return super.getText() + hyperlinkText;
  62. }
  63. }