Anonymous avatar Anonymous committed 4643b42 Draft

added dotdot removal in resources

Comments (0)

Files changed (4)

-<?xml version="1.0" encoding="UTF-8"?>
-<projectDescription>
-	<name>pdf2svg-isaac</name>
-	<comment></comment>
-	<projects>
-	</projects>
-	<buildSpec>
-		<buildCommand>
-			<name>org.eclipse.wst.common.project.facet.core.builder</name>
-			<arguments>
-			</arguments>
-		</buildCommand>
-		<buildCommand>
-			<name>org.eclipse.jdt.core.javabuilder</name>
-			<arguments>
-			</arguments>
-		</buildCommand>
-		<buildCommand>
-			<name>org.eclipse.m2e.core.maven2Builder</name>
-			<arguments>
-			</arguments>
-		</buildCommand>
-		<buildCommand>
-			<name>org.maven.ide.eclipse.maven2Builder</name>
-			<arguments>
-			</arguments>
-		</buildCommand>
-	</buildSpec>
-	<natures>
-		<nature>org.maven.ide.eclipse.maven2Nature</nature>
-		<nature>org.eclipse.m2e.core.maven2Nature</nature>
-		<nature>org.eclipse.jdt.core.javanature</nature>
-		<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
-	</natures>
-</projectDescription>
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>pdf2svg-isaac</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.wst.common.project.facet.core.builder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
+			<name>org.maven.ide.eclipse.maven2Builder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
+			<name>org.eclipse.m2e.core.maven2Builder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.maven.ide.eclipse.maven2Nature</nature>
+		<nature>org.eclipse.m2e.core.maven2Nature</nature>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+		<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
+	</natures>
+</projectDescription>

src/main/java/org/xmlcml/pdf2svg/CodePointSet.java

 import org.apache.log4j.Logger;
 import org.xmlcml.euclid.Util;
 import org.xmlcml.pdf2svg.util.PConstants;
+import org.xmlcml.pdf2svg.util.PDF2SVGUtil;
 
 /** local implementation of codePointSet
  * 
 	}
 
 	public static CodePointSet readCodePointSet(String codePointSetXmlResource) {
+		codePointSetXmlResource = PDF2SVGUtil.normalizeResource(codePointSetXmlResource);
 		CodePointSet codePointSet = new CodePointSet();
 		try {
 			Element codePointSetElement = new Builder().build(

src/main/java/org/xmlcml/pdf2svg/util/PDF2SVGUtil.java

  */
 package org.xmlcml.pdf2svg.util;
 
+import java.util.ArrayList;
+import java.util.List;
+
 import nu.xom.Attribute;
 
 import org.apache.pdfbox.pdmodel.common.PDMatrix;
 	public static final String CHARACTER_NEW_CODE = "newCode";
 	public static final String FONT_ENCODING = "fontEnc";
 	public static final String LIGATURE = "ligature";
+	private static final String DOT = CMLConstants.S_PERIOD;
+	private static final String DOT_DOT = DOT+CMLConstants.S_PERIOD;
 	
 	public static void setSVGXAttribute(SVGElement svgElement, String attName, String value) {
 		if (attName != null && value != null) {
 		return ra;
 	}
 
+	/** removes ./ and ../ from a resources name
+	 * e.g. a/../b => b
+	 * a/./b => a/b
+	 * a/b../../c/d => c/d
+	 * @param resource
+	 * @return
+	 */
+	public static String normalizeResource(String resource) {
+		resource = resource.trim();
+		String[] ss = resource.split(CMLConstants.S_SLASH);
+		List<String> sList = new ArrayList<String>();
+		for (int i = 0; i <ss.length; i++) {
+			if (ss[i].trim().length() == 0){
+				sList.add("");
+			} else if (ss[i].equals(DOT)){
+				continue;
+			} else if (ss[i].equals(DOT_DOT)) {
+				if (sList.size() == 0) {
+					throw new RuntimeException("Cannot start resource with ../ or unbalanced ../");
+				}
+				sList.remove(sList.size()-1);
+			} else {
+				sList.add(ss[i]);
+			}
+		}
+		StringBuilder sb = new StringBuilder();
+		for (int i = 0 ; i < sList.size(); i++) {
+			if (i > 0) {
+				sb.append(CMLConstants.S_SLASH);
+			}
+			sb.append(sList.get(i));
+		}
+		if (resource.endsWith(CMLConstants.S_SLASH)) {
+			sb.append(CMLConstants.S_SLASH);
+		}
+		return sb.toString();
+	}
+
 
 }

src/test/java/org/xmlcml/pdf2svg/util/PDF2SVGUtilTest.java

+package org.xmlcml.pdf2svg.util;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class PDF2SVGUtilTest {
+
+	@Test
+	public void testNormal() {
+		String s = "a/b/c";
+		String s1 = PDF2SVGUtil.normalizeResource(s);
+		Assert.assertEquals(s, s, s1);
+	}
+	
+	@Test
+	public void testSlash() {
+		String s = "a/b/c/";
+		String s1 = PDF2SVGUtil.normalizeResource(s);
+		Assert.assertEquals(s, s, s1);
+	}
+	
+	@Test
+	public void testDot() {
+		String s = "a/./c";
+		String s1 = PDF2SVGUtil.normalizeResource(s);
+		Assert.assertEquals(s, "a/c", s1);
+	}
+	
+	
+	@Test
+	public void testDotDot() {
+		String s = "a/../c";
+		String s1 = PDF2SVGUtil.normalizeResource(s);
+		Assert.assertEquals(s, "c", s1);
+	}
+	
+	@Test
+	public void testDotDotDotDot() {
+		String s = "a/b/../../c/d";
+		String s1 = PDF2SVGUtil.normalizeResource(s);
+		Assert.assertEquals(s, "c/d", s1);
+	}
+	
+	@Test
+	public void testDotDotDotDotSlash() {
+		String s = "a/b/../../c/d/";
+		String s1 = PDF2SVGUtil.normalizeResource(s);
+		Assert.assertEquals(s, "c/d/", s1);
+	}
+	
+	@Test
+	public void testDotDotAtStart() {
+		String s = "../c/d";
+		try {
+			String s1 = PDF2SVGUtil.normalizeResource(s);
+			Assert.fail("should throw RuntimeException");
+		} catch (RuntimeException e) {
+			
+		}
+	}
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.