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.

PDFName.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.pdf;
  19. import java.io.IOException;
  20. import java.io.OutputStream;
  21. import java.io.Serializable;
  22. import org.apache.commons.io.output.CountingOutputStream;
  23. /**
  24. * Class representing a PDF name object.
  25. */
  26. public class PDFName extends PDFObject implements Serializable {
  27. private static final long serialVersionUID = -968412396459739925L;
  28. private String name;
  29. /**
  30. * Creates a new PDF name object.
  31. * @param name the name value
  32. */
  33. public PDFName(String name) {
  34. super();
  35. this.name = escapeName(name);
  36. }
  37. private static final String ESCAPED_NAME_CHARS = "/()<>[]%#";
  38. /**
  39. * Escapes a PDF name. It adds the leading slash and escapes characters as necessary.
  40. * @param name the name
  41. * @return the escaped name
  42. */
  43. static String escapeName(String name) {
  44. StringBuilder sb = new StringBuilder(Math.min(16, name.length() + 4));
  45. boolean skipFirst = false;
  46. sb.append('/');
  47. if (name.startsWith("/")) {
  48. skipFirst = true;
  49. }
  50. for (int i = (skipFirst ? 1 : 0), c = name.length(); i < c; i++) {
  51. char ch = name.charAt(i);
  52. if (ch < 33 || ch > 126 || ESCAPED_NAME_CHARS.indexOf(ch) >= 0) {
  53. sb.append('#');
  54. toHex(ch, sb);
  55. } else {
  56. sb.append(ch);
  57. }
  58. }
  59. return sb.toString();
  60. }
  61. private static final char[] DIGITS
  62. = {'0', '1', '2', '3', '4', '5', '6', '7',
  63. '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
  64. private static void toHex(char ch, StringBuilder sb) {
  65. if (ch >= 256) {
  66. throw new IllegalArgumentException(
  67. "Only 8-bit characters allowed by this implementation");
  68. }
  69. sb.append(DIGITS[ch >>> 4 & 0x0F]);
  70. sb.append(DIGITS[ch & 0x0F]);
  71. }
  72. /** {@inheritDoc} */
  73. @Override
  74. public String toString() {
  75. return this.name;
  76. }
  77. /**
  78. * Returns the name without the leading slash.
  79. * @return the name without the leading slash
  80. */
  81. public String getName() {
  82. return this.name.substring(1);
  83. }
  84. /** {@inheritDoc} */
  85. public boolean equals(Object obj) {
  86. if (!(obj instanceof PDFName)) {
  87. return false;
  88. }
  89. PDFName other = (PDFName)obj;
  90. return this.name.equals(other.name);
  91. }
  92. /** {@inheritDoc} */
  93. public int hashCode() {
  94. return name.hashCode();
  95. }
  96. @Override
  97. public int output(OutputStream stream) throws IOException {
  98. CountingOutputStream cout = new CountingOutputStream(stream);
  99. StringBuilder textBuffer = new StringBuilder(64);
  100. textBuffer.append(toString());
  101. PDFDocument.flushTextBuffer(textBuffer, cout);
  102. return cout.getCount();
  103. }
  104. @Override
  105. public void outputInline(OutputStream out, StringBuilder textBuffer) throws IOException {
  106. if (hasObjectNumber()) {
  107. textBuffer.append(referencePDF());
  108. } else {
  109. textBuffer.append(toString());
  110. }
  111. }
  112. }