1 51 package org.apache.fop.datatypes; 52 53 import org.apache.fop.pdf.PDFGoTo; 54 import org.apache.fop.layout.Area; 55 import org.apache.fop.apps.FOPException; 56 57 import java.util.HashMap ; 59 import java.util.Iterator ; 60 61 76 public class IDReferences { 77 private HashMap idReferences, idValidation, idUnvalidated; 78 79 static final int ID_PADDING = 5000; 81 84 public IDReferences() { 85 idReferences = new HashMap (); 86 idValidation = new HashMap (); 87 idUnvalidated = new HashMap (); 88 } 89 90 91 98 public void initializeID(String id, Area area) throws FOPException { 99 createID(id); 100 configureID(id, area); 101 } 102 103 104 111 public void createID(String id) throws FOPException { 112 if (id != null &&!id.equals("")) { 113 if (doesUnvalidatedIDExist(id)) { 114 removeFromUnvalidatedIDList(id); 115 removeFromIdValidationList(id); 117 } 118 else if (doesIDExist(id)) { 119 throw new FOPException("The id \"" + id 120 + "\" already exists in this document"); 121 } else { 122 createNewId(id); 123 removeFromIdValidationList(id); 124 } 125 126 } 127 } 128 129 134 public void createUnvalidatedID(String id) { 135 if (id != null &&!id.equals("")) { 136 if (!doesIDExist(id)) { 137 createNewId(id); 138 addToUnvalidatedIdList(id); 139 } 140 } 141 } 142 143 150 public void addToUnvalidatedIdList(String id) { 151 idUnvalidated.put(id,""); 152 } 153 154 161 public void removeFromUnvalidatedIDList(String id) { 162 idUnvalidated.remove(id); 163 } 164 165 172 public boolean doesUnvalidatedIDExist(String id) { 173 return idUnvalidated.containsKey(id); 174 } 175 176 182 public void configureID(String id, Area area) { 183 if (id != null && !id.equals("")) { 184 setPosition(id, 185 area.getPage().getBody().getXPosition() 186 + area.getTableCellXOffset() - ID_PADDING, 187 area.getPage().getBody().getYPosition() 188 - area.getAbsoluteHeight() + ID_PADDING); 189 setPageNumber(id, area.getPage().getFormattedNumber()); 190 area.getPage().addToIDList(id); 191 } 192 } 193 194 200 public void addToIdValidationList(String id) { 201 idValidation.put(id, ""); 202 } 203 204 205 206 211 public void removeFromIdValidationList(String id) { 212 idValidation.remove(id); 213 } 214 215 216 221 public void removeID(String id) { 222 idReferences.remove(id); 223 } 224 225 226 231 public boolean isEveryIdValid() { 232 return (idValidation.size() == 0); 233 } 234 235 236 237 242 public String getInvalidIds() { 243 StringBuffer list = new StringBuffer (); 244 Iterator iterator = idValidation.keySet().iterator(); 245 while (iterator.hasNext()) { 246 list.append("\n\"").append(iterator.next().toString()).append("\" "); 247 } 248 return list.toString(); 249 } 250 251 252 258 public boolean doesIDExist(String id) { 259 return idReferences.containsKey(id); 260 } 261 262 263 269 public boolean doesGoToReferenceExist(String id) { 270 IDNode node = (IDNode)idReferences.get(id); 271 return node.isThereInternalLinkGoTo(); 272 } 273 274 275 276 277 283 public String getInternalLinkGoToReference(String id) { 284 IDNode node = (IDNode)idReferences.get(id); 285 return node.getInternalLinkGoToReference(); 286 } 287 288 289 290 298 public String createInternalLinkGoTo(String id, int objectNumber) { 299 IDNode node = (IDNode)idReferences.get(id); node.createInternalLinkGoTo(objectNumber); return node.getInternalLinkGoToReference(); } 303 304 305 306 311 public void createNewId(String id) { 312 IDNode node = new IDNode(id); 313 idReferences.put(id, node); 314 } 315 316 317 323 public PDFGoTo getPDFGoTo(String id) { 324 IDNode node = (IDNode)idReferences.get(id); 325 return node.getInternalLinkGoTo(); 326 } 327 328 329 336 public void setInternalGoToPageReference(String id, 337 String pageReference) { 338 IDNode node = (IDNode)idReferences.get(id); 339 if (node != null) { 340 node.setInternalLinkGoToPageReference(pageReference); 341 } 342 } 343 344 345 351 public void setPageNumber(String id, String pageNumber) { 352 IDNode node = (IDNode)idReferences.get(id); 353 node.setPageNumber(pageNumber); 354 } 355 356 357 363 public String getPageNumber(String id) { 364 if (doesIDExist(id)) { 365 IDNode node = (IDNode)idReferences.get(id); 366 return node.getPageNumber(); 367 } else { 368 addToIdValidationList(id); 369 return null; 370 } 371 } 372 373 374 381 public void setPosition(String id, int x, int y) { 382 IDNode node = (IDNode)idReferences.get(id); 383 node.setPosition(x, y); 384 } 385 386 public Iterator getInvalidElements() { 387 return idValidation.keySet().iterator(); 388 } 389 390 398 public String getDestinationRef(String id) { 399 if (doesIDExist(id)) { 400 IDNode node = (IDNode)idReferences.get(id); 401 return "[ " + node.getPageReference() + " /XYZ " + 402 node.getXPosition()/1000f + " " + node.getYPosition()/1000f + " null ]"; 403 } else { 404 addToIdValidationList(id); 405 return null; 406 } 407 } 408 409 } 410 | Popular Tags |