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.

AbstractNamedAFPObject.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.afp.modca;
  19. import java.io.UnsupportedEncodingException;
  20. import org.apache.fop.afp.AFPConstants;
  21. /**
  22. * This is the base class for all named data stream objects.
  23. * A named data stream object has an 8 byte EBCIDIC name.
  24. */
  25. public abstract class AbstractNamedAFPObject extends AbstractTripletStructuredObject {
  26. private static final int DEFAULT_NAME_LENGTH = 8;
  27. /**
  28. * The actual name of the object
  29. */
  30. protected String name;
  31. /**
  32. * Default constructor
  33. */
  34. protected AbstractNamedAFPObject() {
  35. }
  36. /**
  37. * Constructor for the ActiveEnvironmentGroup, this takes a
  38. * name parameter which should be 8 characters long.
  39. *
  40. * @param name the object name
  41. */
  42. protected AbstractNamedAFPObject(String name) {
  43. this.name = name;
  44. }
  45. /**
  46. * Returns the name length
  47. *
  48. * @return the name length
  49. */
  50. protected int getNameLength() {
  51. return DEFAULT_NAME_LENGTH;
  52. }
  53. /**
  54. * Returns the name as a byte array in EBCIDIC encoding
  55. *
  56. * @return the name as a byte array in EBCIDIC encoding
  57. */
  58. public byte[] getNameBytes() {
  59. int afpNameLen = getNameLength();
  60. int nameLen = name.length();
  61. if (nameLen < afpNameLen) {
  62. name = (name + " ").substring(0, afpNameLen);
  63. } else if (name.length() > afpNameLen) {
  64. String truncatedName = name.substring(nameLen - afpNameLen, nameLen);
  65. LOG.warn("Constructor:: name '" + name + "'"
  66. + " truncated to " + afpNameLen + " chars"
  67. + " ('" + truncatedName + "')");
  68. name = truncatedName;
  69. }
  70. byte[] nameBytes = null;
  71. try {
  72. nameBytes = name.getBytes(AFPConstants.EBCIDIC_ENCODING);
  73. } catch (UnsupportedEncodingException usee) {
  74. nameBytes = name.getBytes();
  75. LOG.warn(
  76. "Constructor:: UnsupportedEncodingException translating the name "
  77. + name);
  78. }
  79. return nameBytes;
  80. }
  81. @Override
  82. protected void copySF(byte[] data, byte type, byte category) {
  83. super.copySF(data, type, category);
  84. byte[] nameData = getNameBytes();
  85. System.arraycopy(nameData, 0, data, 9, nameData.length);
  86. }
  87. /**
  88. * Returns the name of this object
  89. *
  90. * @return the name of this object
  91. */
  92. public String getName() {
  93. return this.name;
  94. }
  95. /**
  96. * Sets the name of this object
  97. *
  98. * @param name the object name
  99. */
  100. public void setName(String name) {
  101. this.name = name;
  102. }
  103. /** {@inheritDoc} */
  104. public String toString() {
  105. return getName();
  106. }
  107. }