1 17 package org.apache.lenya.cms.publication.util; 18 19 import java.io.File ; 20 import java.io.FileFilter ; 21 import java.io.FilenameFilter ; 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import java.util.Arrays ; 25 import java.util.List ; 26 27 import javax.xml.parsers.ParserConfigurationException ; 28 29 import org.apache.avalon.excalibur.io.FileUtil; 30 import org.apache.lenya.cms.publication.Document; 31 import org.apache.lenya.cms.publication.DocumentBuilder; 32 import org.apache.lenya.cms.publication.Publication; 33 import org.apache.lenya.xml.DocumentHelper; 34 import org.apache.xpath.XPathAPI; 35 import org.w3c.dom.Attr ; 36 import org.w3c.dom.Node ; 37 import org.w3c.dom.NodeList ; 38 import org.xml.sax.SAXException ; 39 40 46 public class LinkRewriter { 47 48 private FileFilter directoryFilter = new FileFilter () { 49 50 public boolean accept(File file) { 51 return file.isDirectory(); 52 } 53 }; 54 55 private FileFilter xmlFileFilter = new FileFilter () { 56 57 public boolean accept(File file) { 58 return file.isFile() && FileUtil.getExtension(file.getName()).equals("xml"); 59 } 60 }; 61 62 65 public LinkRewriter() { 66 } 67 68 75 public void rewriteLinks(Document originalTargetDocument, Document newTargetDocument, 76 String contextPath) { 77 78 Publication publication = originalTargetDocument.getPublication(); 79 String area = originalTargetDocument.getArea(); 80 File [] files = getDocumentFiles(publication, area); 81 82 DocumentBuilder builder = publication.getDocumentBuilder(); 83 84 try { 85 for (int fileIndex = 0; fileIndex < files.length; fileIndex++) { 86 org.w3c.dom.Document xmlDocument = DocumentHelper.readDocument(files[fileIndex]); 87 boolean linksRewritten = false; 88 89 String [] xPaths = publication.getRewriteAttributeXPaths(); 90 for (int xPathIndex = 0; xPathIndex < xPaths.length; xPathIndex++) { 91 NodeList nodes = XPathAPI.selectNodeList(xmlDocument, xPaths[xPathIndex]); 92 for (int nodeIndex = 0; nodeIndex < nodes.getLength(); nodeIndex++) { 93 Node node = nodes.item(nodeIndex); 94 if (node.getNodeType() != Node.ATTRIBUTE_NODE) { 95 throw new RuntimeException ("The XPath [" + xPaths[xPathIndex] 96 + "] may only match attribute nodes!"); 97 } 98 Attr attribute = (Attr ) node; 99 final String url = attribute.getValue(); 100 101 if (url.startsWith(contextPath + "/" + publication.getId())) { 102 final String webappUrl = url.substring(contextPath.length()); 103 104 if (builder.isDocument(publication, webappUrl)) { 105 Document targetDocument = builder.buildDocument(publication, webappUrl); 106 107 if (matches(targetDocument, originalTargetDocument)) { 108 String newTargetUrl = getNewTargetURL(targetDocument, 109 originalTargetDocument, 110 newTargetDocument); 111 attribute.setValue(contextPath + newTargetUrl); 112 linksRewritten = true; 113 } 114 } 115 } 116 } 117 } 118 119 if (linksRewritten) { 120 DocumentHelper.writeDocument(xmlDocument, files[fileIndex]); 121 } 122 } 123 } catch (Exception e) { 124 throw new RuntimeException (e); 125 } 126 } 127 128 136 protected boolean matches(Document targetDocument, Document originalTargetDocument) { 137 String matchString = originalTargetDocument.getId() + "/"; 138 String testString = targetDocument.getId() + "/"; 139 return testString.startsWith(matchString); 140 } 141 142 149 protected String getNewTargetURL(Document targetDocument, Document originalTargetDocument, 150 Document newTargetDocument) { 151 String originalId = originalTargetDocument.getId(); 152 String targetId = targetDocument.getId(); 153 String childString = targetId.substring(originalId.length()); 154 155 DocumentBuilder builder = targetDocument.getPublication().getDocumentBuilder(); 156 String newTargetUrl = builder.buildCanonicalUrl(newTargetDocument.getPublication(), 157 newTargetDocument.getArea(), 158 newTargetDocument.getId() + childString, 159 targetDocument.getLanguage()); 160 161 return newTargetUrl; 162 } 163 164 170 protected File [] getDocumentFiles(Publication publication, String area) { 171 File directory = publication.getContentDirectory(area); 172 List files = getDocumentFiles(directory); 173 return (File []) files.toArray(new File [files.size()]); 174 } 175 176 181 protected List getDocumentFiles(File directory) { 182 183 List list = new ArrayList (); 184 185 File [] directories = directory.listFiles(directoryFilter); 186 for (int i = 0; i < directories.length; i++) { 187 list.addAll(getDocumentFiles(directories[i])); 188 } 189 File [] xmlFiles = directory.listFiles(xmlFileFilter); 190 list.addAll(Arrays.asList(xmlFiles)); 191 return list; 192 } 193 194 } | Popular Tags |