You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by hb...@apache.org on 2022/12/29 11:09:05 UTC
[maven-doxia] branch doxia-1.x updated: [DOXIA-662] Non unique IDs generated by IndexingSink
This is an automated email from the ASF dual-hosted git repository.
hboutemy pushed a commit to branch doxia-1.x
in repository https://gitbox.apache.org/repos/asf/maven-doxia.git
The following commit(s) were added to refs/heads/doxia-1.x by this push:
new 24f0066c [DOXIA-662] Non unique IDs generated by IndexingSink
24f0066c is described below
commit 24f0066c4b0f6a1c0ae4b38204f5df716a3d310b
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Thu Jul 7 10:52:24 2022 +0200
[DOXIA-662] Non unique IDs generated by IndexingSink
This closes #41
---
.../org/apache/maven/doxia/index/IndexingSink.java | 36 ++++++++++++++++++++--
.../apache/maven/doxia/index/IndexingSinkTest.java | 36 ++++++++++++++++++++++
2 files changed, 70 insertions(+), 2 deletions(-)
diff --git a/doxia-core/src/main/java/org/apache/maven/doxia/index/IndexingSink.java b/doxia-core/src/main/java/org/apache/maven/doxia/index/IndexingSink.java
index cfa8bddf..b5b33f28 100644
--- a/doxia-core/src/main/java/org/apache/maven/doxia/index/IndexingSink.java
+++ b/doxia-core/src/main/java/org/apache/maven/doxia/index/IndexingSink.java
@@ -19,7 +19,10 @@ package org.apache.maven.doxia.index;
* under the License.
*/
+import java.util.HashMap;
+import java.util.Map;
import java.util.Stack;
+import java.util.concurrent.atomic.AtomicInteger;
import org.apache.maven.doxia.sink.impl.SinkAdapter;
import org.apache.maven.doxia.util.HtmlTools;
@@ -69,6 +72,12 @@ public class IndexingSink
/** The stack. */
private final Stack<IndexEntry> stack;
+ /** A map containing all used ids of index entries as key and how often they are used as value
+ * (0-based, i.e. 0 means used 1 time). {@link AtomicInteger} is only used here as it implements
+ * a mutable integer (not for its atomicity).
+ */
+ private final Map<String, AtomicInteger> usedIds;
+
/**
* Default constructor.
*
@@ -78,7 +87,8 @@ public class IndexingSink
{
stack = new Stack<>();
stack.push( sectionEntry );
-
+ usedIds = new HashMap<>();
+ usedIds.put( sectionEntry.getId(), new AtomicInteger() );
init();
}
@@ -311,7 +321,7 @@ public class IndexingSink
title = title.replaceAll( "[\\r\\n]+", "" );
entry.setTitle( title );
- entry.setId( HtmlTools.encodeId( title ) );
+ entry.setId( getUniqueId ( HtmlTools.encodeId( title ) ) );
break;
// Dunno how to handle these yet
@@ -323,6 +333,28 @@ public class IndexingSink
}
}
+ /**
+ * Converts the given id into a unique one by potentially suffixing it with an index value.
+ *
+ * @param id
+ * @return the unique id
+ */
+ String getUniqueId( String id )
+ {
+ final String uniqueId;
+
+ if ( usedIds.containsKey( id ) )
+ {
+ uniqueId = id + "_" + usedIds.get( id ).incrementAndGet();
+ }
+ else
+ {
+ usedIds.put( id, new AtomicInteger() );
+ uniqueId = id;
+ }
+ return uniqueId;
+ }
+
/**
* Creates and pushes a new IndexEntry onto the top of this stack.
*/
diff --git a/doxia-core/src/test/java/org/apache/maven/doxia/index/IndexingSinkTest.java b/doxia-core/src/test/java/org/apache/maven/doxia/index/IndexingSinkTest.java
new file mode 100644
index 00000000..e59e7a8d
--- /dev/null
+++ b/doxia-core/src/test/java/org/apache/maven/doxia/index/IndexingSinkTest.java
@@ -0,0 +1,36 @@
+package org.apache.maven.doxia.index;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class IndexingSinkTest
+{
+ @Test
+ public void testGetUniqueId()
+ {
+ IndexingSink sink = new IndexingSink( new IndexEntry( "root" ) );
+ assertEquals( "root_1", sink.getUniqueId( "root" ) );
+ assertEquals( "root_2", sink.getUniqueId( "root" ) );
+ assertEquals( "newid", sink.getUniqueId( "newid" ) );
+ }
+}