Skip to content

Commit

Permalink
[Sitemaps] Google Sitemap PageMap extensions, implements crawler-comm…
Browse files Browse the repository at this point in the history
…ons#388

- address review comments
  - move parts of the PageMap.asMap() to PageMapDataObject.asMap()
  - add missing license header
  • Loading branch information
sebastian-nagel committed Oct 24, 2023
1 parent aee49e1 commit 835fafe
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
11 changes: 3 additions & 8 deletions src/main/java/crawlercommons/sitemaps/extension/PageMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
Expand Down Expand Up @@ -48,14 +47,10 @@ public void addDataObject(PageMapDataObject d) {
@Override
public Map<String, String[]> asMap() {
Map<String, String[]> map = new LinkedHashMap<>();

String keyFormat = "%d::%s::%s";
String valueFormat = "%s: %s";
int i = 0;
for (PageMapDataObject dobj : dataObjects) {
String key = String.format(Locale.ROOT, keyFormat, i, (dobj.getType() == null ? "" : dobj.getType()), (dobj.getId() == null ? "" : dobj.getId()));
String[] values = dobj.getAttributes().entrySet().stream().map((Entry<String, String> e) -> String.format(Locale.ROOT, valueFormat, e.getKey(), e.getValue())).toArray(String[]::new);
map.put(key, values);
for (Entry<String, String[]> e : dobj.asMap().entrySet()) {
map.put(e.getKey(), e.getValue());
}
}
return map;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
package crawlercommons.sitemaps.extension;

import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Map.Entry;

@SuppressWarnings("serial")
public class PageMapDataObject extends ExtensionMetadata {
Expand Down Expand Up @@ -79,7 +81,10 @@ public String toString() {

@Override
public Map<String, String[]> asMap() {
// TODO Auto-generated method stub
return null;
String keyFormat = "%s::%s";
String valueFormat = "%s: %s";
String key = String.format(Locale.ROOT, keyFormat, (getType() == null ? "" : getType()), (getId() == null ? "" : getId()));
String[] values = getAttributes().entrySet().stream().map((Entry<String, String> e) -> String.format(Locale.ROOT, valueFormat, e.getKey(), e.getValue())).toArray(String[]::new);
return Map.of(key, values);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class PageMapsHandler extends ExtensionHandler {
private PageMap currPageMap;
private PageMapDataObject currDataObj;
private String currAttrName;
private StringBuilder currAttrVal = new StringBuilder();;
private StringBuilder currAttrVal = new StringBuilder();
private String currAttrValFromAttr;

public PageMapsHandler() {
Expand Down
19 changes: 17 additions & 2 deletions src/test/java/crawlercommons/sitemaps/extension/PageMapsTest.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/**
* Copyright 2023 Crawler-Commons
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package crawlercommons.sitemaps.extension;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -38,8 +53,8 @@ public void testPageMapAttributesEquals() {
assertEquals("bar", b.getPageMapDataObjects().get(0).getAttribute("foo"));
assertEquals("world", b.getPageMapDataObjects().get(0).getAttribute("hello"));
assertEquals(1, b.asMap().size());
assertNotNull(b.asMap().get("0::test::a"));
assertEquals(2, b.asMap().get("0::test::a").length);
assertNotNull(b.asMap().get("test::a"));
assertEquals(2, b.asMap().get("test::a").length);

PageMap c = new PageMap();
PageMapDataObject dc = new PageMapDataObject("test", "c");
Expand Down

0 comments on commit 835fafe

Please sign in to comment.