Skip to content

Commit

Permalink
fix: solve build failed error
Browse files Browse the repository at this point in the history
  • Loading branch information
LMay001 committed Jan 18, 2024
1 parent 82d0929 commit 9e924e1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
48 changes: 47 additions & 1 deletion src/main/java/org/casbin/jcasbin/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.casbin.jcasbin.log.*;
import org.casbin.jcasbin.util.Util;

import java.util.regex.Pattern;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
Expand Down Expand Up @@ -243,7 +244,7 @@ private String saveSectionToText(String sec) {
}

for (Map.Entry<String, Assertion> entry : section.entrySet()) {
res.append(String.format("%s = %s\n", entry.getKey(), entry.getValue().value));
res.append(String.format("%s = %s\n", entry.getKey(), entry.getValue().value.replace("_", ".")));
}

return res.toString();
Expand Down Expand Up @@ -386,4 +387,49 @@ public enum PolicyOperations {
POLICY_ADD,
POLICY_REMOVE
}

public String toText() {
Map<String, String> tokenPatterns = new HashMap<>();

Pattern pPattern = Pattern.compile("^p_");
Pattern rPattern = Pattern.compile("^r_");

for (String ptype : new String[]{"r", "p"}) {
for (String token : model.get(ptype).get(ptype).tokens) {
String newToken = rPattern.matcher(pPattern.matcher(token).replaceAll("p.")).replaceAll("r.");
tokenPatterns.put(token, newToken);
}
}

if (model.get("e").get("e").value.contains("p_eft")) {
tokenPatterns.put("p_eft", "p.eft");
}

StringBuilder s = new StringBuilder();
writeString(s, "r", tokenPatterns);
writeString(s, "p", tokenPatterns);

if (model.containsKey("g")) {
s.append("[role_definition]\n");
for (String ptype : model.get("g").keySet()) {
s.append(String.format("%s = %s\n", ptype, model.get("g").get(ptype).value));
}
}

writeString(s, "e", tokenPatterns);
writeString(s, "m", tokenPatterns);

return s.toString();
}

private void writeString(StringBuilder s, String sec, Map<String, String> tokenPatterns) {
s.append(String.format("[%s]\n", sectionNameMap.get(sec)));
for (String ptype : model.get(sec).keySet()) {
String value = model.get(sec).get(ptype).value;
for (Map.Entry<String, String> entry : tokenPatterns.entrySet()) {
value = value.replace(entry.getKey(), entry.getValue());
}
s.append(String.format("%s = %s\n", sec, value));
}
}
}
4 changes: 2 additions & 2 deletions src/test/java/org/casbin/jcasbin/main/ModelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ private void testModelToText(String mData, String mExpected) {
}

Model newM = new Model();
System.out.println(m.saveModelToText());
newM.loadModelFromText(m.saveModelToText());
System.out.println(m.toText());
newM.loadModelFromText(m.toText());

for (int i = 0; i < ptypes.length; i++) {
assertEquals(expectedValues[i], newM.model.get(ptypes[i]).get(ptypes[i]).value);
Expand Down

0 comments on commit 9e924e1

Please sign in to comment.