changeset 122:8d79021099a4

XML dump with new ismi-date element.
author Robert Casties <casties@mpiwg-berlin.mpg.de>
date Sun, 29 Jan 2023 19:43:02 +0100
parents 0a8facc3d296
children ba3742584f93
files .hgignore pom.xml src/main/java/org/mpi/openmind/repository/utils/OM4StreamWriter.java src/main/java/org/mpi/openmind/repository/utils/XMLUtil.java src/main/java/org/mpi/openmind/repository/utils/ismi/ISMICalendar.java src/main/java/org/mpi/openmind/repository/utils/ismi/ISMIDate.java
diffstat 6 files changed, 198 insertions(+), 78 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Wed Jan 26 17:39:12 2022 +0100
+++ b/.hgignore	Sun Jan 29 19:43:02 2023 +0100
@@ -20,4 +20,6 @@
 syntax: regexp
 ^src/main/resources/openmind\.properties$
 syntax: regexp
-^bin$
\ No newline at end of file
+^bin$
+syntax: regexp
+^data$
\ No newline at end of file
--- a/pom.xml	Wed Jan 26 17:39:12 2022 +0100
+++ b/pom.xml	Sun Jan 29 19:43:02 2023 +0100
@@ -109,7 +109,8 @@
 		<dependency>
 		    <groupId>mysql</groupId>
 		    <artifactId>mysql-connector-java</artifactId>
-		    <version>5.1.6</version>
+		    <!-- <version>5.1.6</version> -->
+		    <version>8.0.19</version>
 		</dependency>		
 		
 		<dependency>
@@ -150,7 +151,18 @@
 		    <version>5.5.1</version>
 		</dependency>
 
-
+<!-- impl necessary to run mvn exec:java -Dexec.mainClass=org.mpi.openmind.scripts.IsmiXmlExport 
+<dependency>
+    <groupId>xerces</groupId>
+    <artifactId>xercesImpl</artifactId>
+    <version>2.12.2</version>
+</dependency>
+<dependency>
+    <groupId>com.fasterxml.woodstox</groupId>
+    <artifactId>woodstox-core</artifactId>
+    <version>5.0.2</version>
+</dependency>
+-->
 		<!-- <dependency>
 			<groupId>javax.servlet</groupId>
 			<artifactId>servlet-api</artifactId>
--- a/src/main/java/org/mpi/openmind/repository/utils/OM4StreamWriter.java	Wed Jan 26 17:39:12 2022 +0100
+++ b/src/main/java/org/mpi/openmind/repository/utils/OM4StreamWriter.java	Sun Jan 29 19:43:02 2023 +0100
@@ -27,6 +27,7 @@
 import org.mpi.openmind.repository.bo.Node;
 import org.mpi.openmind.repository.bo.Relation;
 import org.mpi.openmind.repository.services.PersistenceService;
+import org.mpi.openmind.repository.utils.ismi.ISMICalendar;
 
 /**
  * Export all entities and relations and definitions to XML.
@@ -39,7 +40,7 @@
  */
 public class OM4StreamWriter {
 
-    protected static final String FORMAT_VERSION = "4.12";
+    protected static final String FORMAT_VERSION = "4.13";
 
     private static Logger logger = Logger.getLogger(OM4StreamWriter.class);
 
@@ -52,7 +53,7 @@
     private static final String ENT_KEY = "<entity-count>";
 
     /** formatter for isodate tag */
-    public static DateTimeFormatter dateFormatter = ISODateTimeFormat.date();
+    public static DateTimeFormatter isodateFormatter = ISODateTimeFormat.date();
     
     /** pattern for bibid in endnote-id attribute */
     public static final Pattern bibidPattern = Pattern.compile("#(\\d+)");
@@ -422,10 +423,12 @@
          * write value as content
          */
         String ov = att.getValue();
+        String ct = att.getContentType();
 		if (StringUtils.isNotEmpty(ov)) {
             writer.writeCharacters(ov);
             String nov = att.getNormalizedOwnValue();
-			if (includeNorm && StringUtils.isNotEmpty(nov) && !ov.equals(nov)) {
+			if (includeNorm && StringUtils.isNotEmpty(nov) && !ov.equals(nov) 
+			        && !(ct != null && ct.equals("date"))) {
                 // write normalized value
             	writer.writeStartElement(XMLUtil.NORMALIZED);
             	writer.writeCharacters(nov);
@@ -443,26 +446,14 @@
                     processed = true;
                 }
             }
-			// convert any date JSON into additional isodate element
+			// convert any date JSON
 			if (!processed && ov.startsWith("{")) {
 				try {
 					JSONObject json = new JSONObject(ov);
-					JSONObject date = null;
-					if (json.has("date")) {
-						date = json.getJSONObject("date"); 
-					} else if (json.has("from")) {
-						date = json.getJSONObject("from"); 
-					}
-					if (date != null) {
-						int year = date.getInt("year");
-						int month = date.getInt("month");
-						int day = date.getInt("dayOfMonth");
-						DateTime dt = new DateTime(year, month, day, 0, 0);
-						writer.writeStartElement(XMLUtil.ISODATE);
-						writer.writeCharacters(dateFormatter.print(dt));
-		            	writer.writeEndElement();
-		            	processed = true;
-					}
+					// convert to simple isodate element
+					processed = writeSimpleDate(writer, json);
+                    // convert to full ismi-date element
+                    processed = writeIsmiDate(writer, json);
 				} catch (JSONException e) {
 					// maybe not JSON...
 				}
@@ -472,7 +463,82 @@
         writer.writeEndElement();
     }
 
+
+    /**
+     * Parse JSON date object and write simple isodate element.
+     * 
+     * @param writer
+     * @param json
+     * @return
+     * @throws JSONException
+     * @throws XMLStreamException
+     */
+    private static boolean writeSimpleDate(XMLStreamWriter writer, JSONObject json)
+            throws JSONException, XMLStreamException {
+        JSONObject date = null;
+        if (json.has("date")) {
+        	date = json.getJSONObject("date"); 
+        } else if (json.has("from")) {
+        	date = json.getJSONObject("from"); 
+        }
+        if (date != null) {
+        	int year = date.getInt("year");
+        	int month = date.getInt("month");
+        	int day = date.getInt("dayOfMonth");
+        	DateTime dt = new DateTime(year, month, day, 0, 0);
+        	writer.writeStartElement(XMLUtil.ISODATE);
+        	writer.writeCharacters(isodateFormatter.print(dt));
+        	writer.writeEndElement();
+        	return true;
+        }
+        return false;
+    }
+
 	
+    /**
+     * Parse JSON date object and write ismi-date element.
+     * 
+     * @param writer
+     * @param json
+     * @return
+     * @throws JSONException
+     * @throws XMLStreamException
+     */
+    private static boolean writeIsmiDate(XMLStreamWriter writer, JSONObject json)
+            throws JSONException, XMLStreamException {
+        ISMICalendar date = new ISMICalendar(json);
+        String state = date.getState();
+        if (state.equals(ISMICalendar.STATE_NOT_CHECKED)) {
+            writer.writeStartElement(XMLUtil.ISMIDATE);
+            writer.writeAttribute(XMLUtil.TYPE, "unspecified");
+            writer.writeCharacters(date.getDateInText());
+            writer.writeEndElement();
+            return true;
+        }
+        if (state.equals(ISMICalendar.STATE_KNOWN)) {
+            writer.writeStartElement(XMLUtil.ISMIDATE);
+            String type = date.getInputForm().toLowerCase();
+            writer.writeAttribute(XMLUtil.TYPE, type);
+            String calendar = date.getCalendarType().toLowerCase();
+            writer.writeAttribute("calendar", calendar);
+            String notes = date.getAdditionalInfo();
+            if (notes != null && !notes.isEmpty()) {
+                writer.writeAttribute("notes", notes);
+            }
+            if (type.equals("year") || type.equals("range")) {
+                writer.writeAttribute("from", date.getFromGregorian().getDateTime().toString(isodateFormatter));
+                writer.writeAttribute("until", date.getUntilGregorian().getDateTime().toString(isodateFormatter));
+            } else if (type.equals("date")) {
+                writer.writeAttribute("date", date.getFromGregorian().getDateTime().toString(isodateFormatter));
+            }
+            writer.writeCharacters(date.toShortString());
+            writer.writeEndElement();
+            return true;
+        }
+        return false;
+    }
+
+    
     private static void writeStats(String statsTag, String entryTag, Map<String, Map<String, Long>> nodeStats,
             XMLStreamWriter writer) throws XMLStreamException {
         // write stats tag
--- a/src/main/java/org/mpi/openmind/repository/utils/XMLUtil.java	Wed Jan 26 17:39:12 2022 +0100
+++ b/src/main/java/org/mpi/openmind/repository/utils/XMLUtil.java	Sun Jan 29 19:43:02 2023 +0100
@@ -97,6 +97,7 @@
     
     public static String ISODATE = "isodate";
     public static String BIBID = "bibid";
+    public static String ISMIDATE = "ismi-date";
 
     public static Node getNodeByName(NodeList nodeList, String name){
         for(int i=0; i < nodeList.getLength(); i++){
--- a/src/main/java/org/mpi/openmind/repository/utils/ismi/ISMICalendar.java	Wed Jan 26 17:39:12 2022 +0100
+++ b/src/main/java/org/mpi/openmind/repository/utils/ismi/ISMICalendar.java	Sun Jan 29 19:43:02 2023 +0100
@@ -63,61 +63,7 @@
 		if(StringUtils.isNotEmpty(jsonString)){
 			try {
 				JSONObject json = new JSONObject(jsonString);
-				this.state = json.getString(STATE);
-				if(StringUtils.isNotEmpty(state)){
-					if(state.equals(STATE_KNOWN)){
-						
-						this.additionalInfo = json.getString(ADD_INF);
-						this.calendarType = json.getString(CALENDAR_TYPE);
-						this.inputForm = json.getString(INPUT_FORM);
-						
-						if(inputForm.equals(INPUT_FORM_YEAR)){
-							this.fromGregorian = new ISMIDate(json.getJSONObject(FROM));
-							this.untilGregorian = new ISMIDate(json.getJSONObject(UNTIL));
-							this.currentYear = json.getInt(YEAR);
-						}else if(inputForm.equals(INPUT_FORM_RANGE)){
-							this.fromGregorian = new ISMIDate(json.getJSONObject(FROM));
-							this.untilGregorian = new ISMIDate(json.getJSONObject(UNTIL));
-							if(calendarType.equals(TYPE_GREGORIAN)){
-								this.currentFrom = new ISMIDate(json.getJSONObject(FROM));
-								this.currentUntil  = new ISMIDate(json.getJSONObject(UNTIL));
-							}else if(calendarType.equals(TYPE_ISLAMIC)){
-								this.currentFrom = new ISMIDate(this.fromGregorian.getIslamicDateTime());
-								this.currentUntil = new ISMIDate(this.untilGregorian.getIslamicDateTime());
-							}else if(calendarType.equals(TYPE_JULIAN)){
-								this.currentFrom = new ISMIDate(this.fromGregorian.getJulianDateTime());
-								this.currentUntil = new ISMIDate(this.untilGregorian.getJulianDateTime());
-							}
-						}else if(inputForm.equals(INPUT_FORM_DATE)){
-							this.fromGregorian = new ISMIDate(json.getJSONObject(DATE));
-							if(calendarType.equals(TYPE_GREGORIAN)){
-								this.currentFrom = new ISMIDate(json.getJSONObject(DATE));
-							}else if(calendarType.equals(TYPE_ISLAMIC)){
-								this.currentFrom = new ISMIDate(this.fromGregorian.getIslamicDateTime());
-							}else if(calendarType.equals(TYPE_JULIAN)){
-								this.currentFrom = new ISMIDate(this.fromGregorian.getJulianDateTime());
-							}
-						}	
-						
-						if(json.has("dayOfWeekFrom")){
-							this.currentFrom.setDayOfWeek(json.getInt("dayOfWeekFrom"));
-						}
-						if(json.has("dayOfWeekUntil")){
-							this.currentUntil.setDayOfWeek(json.getInt("dayOfWeekUntil"));
-						}
-						if(json.has("dayOfWeek")){
-							this.currentFrom.setDayOfWeek(json.getInt("dayOfWeek"));
-						}
-						
-					}else if(state.equals(STATE_NOT_CHECKED) || state.equals(STATE_UNKNOWN)){
-						if(json.has(DATE_IN_TEXT)){
-							this.dateInText = json.getString(DATE_IN_TEXT);	
-						}
-						this.calendarType = TYPE_GREGORIAN;
-						this.inputForm = INPUT_FORM_YEAR;
-					}
-				}
-				
+				fromJSON(json);
 			} catch (Exception e) {
 				e.printStackTrace();
 				this.state = STATE_NOT_CHECKED;
@@ -130,6 +76,67 @@
 			this.state = STATE_NOT_CHECKED;
 		}
 	}
+
+	public ISMICalendar(JSONObject json) throws JSONException {
+	    fromJSON(json);
+	}
+	
+    private void fromJSON(JSONObject json) throws JSONException {
+        this.state = json.getString(STATE);
+        if(StringUtils.isNotEmpty(state)){
+        	if(state.equals(STATE_KNOWN)){
+        		
+        		this.additionalInfo = json.getString(ADD_INF);
+        		this.calendarType = json.getString(CALENDAR_TYPE);
+        		this.inputForm = json.getString(INPUT_FORM);
+        		
+        		if(inputForm.equals(INPUT_FORM_YEAR)){
+        			this.fromGregorian = new ISMIDate(json.getJSONObject(FROM));
+        			this.untilGregorian = new ISMIDate(json.getJSONObject(UNTIL));
+        			this.currentYear = json.getInt(YEAR);
+        		}else if(inputForm.equals(INPUT_FORM_RANGE)){
+        			this.fromGregorian = new ISMIDate(json.getJSONObject(FROM));
+        			this.untilGregorian = new ISMIDate(json.getJSONObject(UNTIL));
+        			if(calendarType.equals(TYPE_GREGORIAN)){
+        				this.currentFrom = new ISMIDate(json.getJSONObject(FROM));
+        				this.currentUntil  = new ISMIDate(json.getJSONObject(UNTIL));
+        			}else if(calendarType.equals(TYPE_ISLAMIC)){
+        				this.currentFrom = new ISMIDate(this.fromGregorian.getIslamicDateTime());
+        				this.currentUntil = new ISMIDate(this.untilGregorian.getIslamicDateTime());
+        			}else if(calendarType.equals(TYPE_JULIAN)){
+        				this.currentFrom = new ISMIDate(this.fromGregorian.getJulianDateTime());
+        				this.currentUntil = new ISMIDate(this.untilGregorian.getJulianDateTime());
+        			}
+        		}else if(inputForm.equals(INPUT_FORM_DATE)){
+        			this.fromGregorian = new ISMIDate(json.getJSONObject(DATE));
+        			if(calendarType.equals(TYPE_GREGORIAN)){
+        				this.currentFrom = new ISMIDate(json.getJSONObject(DATE));
+        			}else if(calendarType.equals(TYPE_ISLAMIC)){
+        				this.currentFrom = new ISMIDate(this.fromGregorian.getIslamicDateTime());
+        			}else if(calendarType.equals(TYPE_JULIAN)){
+        				this.currentFrom = new ISMIDate(this.fromGregorian.getJulianDateTime());
+        			}
+        		}	
+        		
+        		if(json.has("dayOfWeekFrom")){
+        			this.currentFrom.setDayOfWeek(json.getInt("dayOfWeekFrom"));
+        		}
+        		if(json.has("dayOfWeekUntil")){
+        			this.currentUntil.setDayOfWeek(json.getInt("dayOfWeekUntil"));
+        		}
+        		if(json.has("dayOfWeek")){
+        			this.currentFrom.setDayOfWeek(json.getInt("dayOfWeek"));
+        		}
+        		
+        	}else if(state.equals(STATE_NOT_CHECKED) || state.equals(STATE_UNKNOWN)){
+        		if(json.has(DATE_IN_TEXT)){
+        			this.dateInText = json.getString(DATE_IN_TEXT);	
+        		}
+        		this.calendarType = TYPE_GREGORIAN;
+        		this.inputForm = INPUT_FORM_YEAR;
+        	}
+        }
+    }
 	
 	public void update(){
 		
@@ -167,6 +174,28 @@
 		return (!this.state.equals(STATE_NOT_CHECKED) && !this.state.equals(STATE_UNKNOWN));
 	}
 	
+	public String toShortString() {
+	    String str = "";
+	    if (inputForm == null) return "UNKNOWN TYPE";
+	    if (inputForm.equals(INPUT_FORM_DATE)) {
+	        str = currentFrom.toShortString();
+	    } else if (inputForm.equals(INPUT_FORM_RANGE)) {
+            str = currentFrom.toShortString()
+                    + " - "
+                    + currentUntil.toShortString();
+	    } else if (inputForm.equals(INPUT_FORM_YEAR)) {
+	        str = currentYear.toString();
+	    }
+	    if (calendarType.equals(TYPE_ISLAMIC)) {
+	        str += " AH";
+	    } else if (calendarType.equals(TYPE_JULIAN)) {
+            str += " CE(J)";
+        } else if (calendarType.equals(TYPE_GREGORIAN)) {
+            str += " CE";
+        }
+	    return str;
+	}
+	
 	public String getFormattedRange(){
 		if(inputForm != null){
 			String from = this.fromGregorian.toString();
--- a/src/main/java/org/mpi/openmind/repository/utils/ismi/ISMIDate.java	Wed Jan 26 17:39:12 2022 +0100
+++ b/src/main/java/org/mpi/openmind/repository/utils/ismi/ISMIDate.java	Sun Jan 29 19:43:02 2023 +0100
@@ -185,6 +185,10 @@
 		return sb.toString();
 	}
 	
+	public String toShortString() {
+	    return String.format("%04d-%02d-%02d", year, month, dayOfMonth);
+	}
+	
 	public String toIslamicString(){
 		try{
 			if(year == null || month == null || dayOfMonth == null)
@@ -237,6 +241,12 @@
 		return "no valid";
 	}
 	
+	public DateTime getDateTime() {
+        if (year == null || month == null || dayOfMonth == null)
+            return null;
+	    return new DateTime(this.year, this.month, this.dayOfMonth, 0, 0, 0, 0, GregorianChronology.getInstance());
+	}
+	
 	public DateTime getIslamicDateTime(){
 		if(year == null || month == null || dayOfMonth == null)
 			return null;