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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.Writer;
  22. import org.apache.commons.io.output.CountingOutputStream;
  23. /**
  24. * Class representing a PDF name object.
  25. */
  26. public class PDFName extends PDFObject {
  27. private String name;
  28. /**
  29. * Creates a new PDF name object.
  30. * @param name the name value
  31. */
  32. public PDFName(String name) {
  33. super();
  34. this.name = escapeName(name);
  35. }
  36. /**
  37. * Escapes a PDF name. It adds the leading slash and escapes characters as necessary.
  38. * @param name the name
  39. * @return the escaped name
  40. */
  41. static String escapeName(String name) {
  42. StringBuffer sb = new StringBuffer(Math.min(16, name.length() + 4));
  43. if (!name.startsWith("/")) {
  44. sb.append('/');
  45. }
  46. for (int i = 0, c = name.length(); i < c; i++) {
  47. char ch = name.charAt(i);
  48. if (ch < 33 || ch > 126 || ch == 0x2F) {
  49. sb.append('#');
  50. toHex(ch, sb);
  51. } else {
  52. sb.append(ch);
  53. }
  54. }
  55. return sb.toString();
  56. }
  57. private static final char[] DIGITS
  58. = {'0', '1', '2', '3', '4', '5', '6', '7',
  59. '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
  60. private static void toHex(char ch, StringBuffer sb) {
  61. if (ch >= 256) {
  62. throw new IllegalArgumentException(
  63. "Only 8-bit characters allowed by this implementation");
  64. }
  65. sb.append(DIGITS[ch >>> 4 & 0x0F]);
  66. sb.append(DIGITS[ch & 0x0F]);
  67. }
  68. /** {@inheritDoc} */
  69. public String toString() {
  70. return this.name;
  71. }
  72. /**
  73. * Returns the name without the leading slash.
  74. * @return the name without the leading slash
  75. */
  76. public String getName() {
  77. return this.name.substring(1);
  78. }
  79. /** {@inheritDoc} */
  80. public boolean equals(Object obj) {
  81. if (!(obj instanceof PDFName)) {
  82. return false;
  83. }
  84. PDFName other = (PDFName)obj;
  85. return this.name.equals(other.name);
  86. }
  87. /** {@inheritDoc} */
  88. public int hashCode() {
  89. return name.hashCode();
  90. }
  91. /** {@inheritDoc} */
  92. protected int output(OutputStream stream) throws IOException {
  93. CountingOutputStream cout = new CountingOutputStream(stream);
  94. Writer writer = PDFDocument.getWriterFor(cout);
  95. if (hasObjectNumber()) {
  96. writer.write(getObjectID());
  97. }
  98. writer.write(toString());
  99. if (hasObjectNumber()) {
  100. writer.write("\nendobj\n");
  101. }
  102. writer.flush();
  103. return cout.getCount();
  104. }
  105. /** {@inheritDoc} */
  106. public void outputInline(OutputStream out, Writer writer) throws IOException {
  107. if (hasObjectNumber()) {
  108. writer.write(referencePDF());
  109. } else {
  110. writer.write(toString());
  111. }
  112. }
  113. }