fix handling package_name for C++ am: 0ec22385b1
am: e1e138fef3

Change-Id: Ib83e120699023c4bf5601a33884fef176ea138ea
diff --git a/build/xsdc.go b/build/xsdc.go
index d336b53..f66476b 100644
--- a/build/xsdc.go
+++ b/build/xsdc.go
@@ -143,9 +143,9 @@
 		},
 	})
 
-	pkgName = strings.Replace(pkgName, ".", "_", -1)
-	module.genOutputs_c = android.PathForModuleGen(ctx, "cpp", pkgName+".cpp")
-	module.genOutputs_h = android.PathForModuleGen(ctx, "cpp", "include/"+pkgName+".h")
+	filenameStem := strings.Replace(pkgName, ".", "_", -1)
+	module.genOutputs_c = android.PathForModuleGen(ctx, "cpp", filenameStem+".cpp")
+	module.genOutputs_h = android.PathForModuleGen(ctx, "cpp", "include/"+filenameStem+".h")
 	module.genOutputDir = android.PathForModuleGen(ctx, "cpp", "include")
 
 	ctx.Build(pctx, android.BuildParams{
diff --git a/src/com/android/xsdc/Main.java b/src/com/android/xsdc/Main.java
index b0fe084..b8f6dd3 100644
--- a/src/com/android/xsdc/Main.java
+++ b/src/com/android/xsdc/Main.java
@@ -105,8 +105,7 @@
             File includeDir = new File(Paths.get(outDir, "include").toString());
             includeDir.mkdirs();
             FileSystem fs = new FileSystem(new File(outDir));
-            CppCodeGenerator cppCodeGenerator = new CppCodeGenerator(xmlSchema,
-                    packageName.replace(".", "_"));
+            CppCodeGenerator cppCodeGenerator = new CppCodeGenerator(xmlSchema, packageName);
             cppCodeGenerator.print(fs);
         }
     }
diff --git a/src/com/android/xsdc/cpp/CppCodeGenerator.java b/src/com/android/xsdc/cpp/CppCodeGenerator.java
index 7a2ca5a..9f9ef7f 100644
--- a/src/com/android/xsdc/cpp/CppCodeGenerator.java
+++ b/src/com/android/xsdc/cpp/CppCodeGenerator.java
@@ -35,16 +35,16 @@
 
 public class CppCodeGenerator {
     private XmlSchema xmlSchema;
-    private String fileName;
+    private String pkgName;
     private Map<String, CppSimpleType> cppSimpleTypeMap;
     private CodeWriter cppFile;
     private CodeWriter headerFile;
     private boolean hasAttr;
 
-    public CppCodeGenerator(XmlSchema xmlSchema, String fileName)
+    public CppCodeGenerator(XmlSchema xmlSchema, String pkgName)
             throws CppCodeGeneratorException {
         this.xmlSchema = xmlSchema;
-        this.fileName = fileName;
+        this.pkgName = pkgName;
 
         // class naming validation
         {
@@ -92,11 +92,14 @@
     public void print(FileSystem fs)
             throws CppCodeGeneratorException, IOException {
         // cpp file, headr file init
-        cppFile =  new CodeWriter(fs.getPrintWriter(fileName + ".cpp"));
-        headerFile = new CodeWriter(fs.getPrintWriter("include/" + fileName + ".h"));
+        String cppFileName = pkgName.replace(".", "_") + ".cpp";
+        String hFileName =  pkgName.replace(".", "_") + ".h";
+        cppFile =  new CodeWriter(fs.getPrintWriter(cppFileName));
+        headerFile = new CodeWriter(fs.getPrintWriter("include/" + hFileName));
 
-        headerFile.printf("#ifndef %s_H\n", fileName.toUpperCase());
-        headerFile.printf("#define %s_H\n\n", fileName.toUpperCase());
+        String headerMacro = hFileName.toUpperCase().replace(".", "_");
+        headerFile.printf("#ifndef %s\n", headerMacro);
+        headerFile.printf("#define %s\n\n", headerMacro);
         headerFile.printf("#include <libxml/parser.h>\n");
         headerFile.printf("#include <libxml/xinclude.h>\n\n");
         headerFile.printf("#include <map>\n");
@@ -104,15 +107,21 @@
         headerFile.printf("#include <string>\n");
         headerFile.printf("#include <vector>\n\n");
 
-        cppFile.printf("#define LOG_TAG \"%s\"\n\n", fileName);
+        cppFile.printf("#define LOG_TAG \"%s\"\n\n", pkgName);
         cppFile.printf("#include <android/log.h>\n");
         cppFile.printf("#include <android-base/strings.h>\n\n");
         cppFile.printf("#include <libxml/parser.h>\n");
         cppFile.printf("#include <libxml/xinclude.h>\n\n");
-        cppFile.printf("#include \"%s.h\"\n\n",fileName);
+        cppFile.printf("#include \"%s\"\n\n", hFileName);
 
         List<String> namespace = new java.util.ArrayList<>();
-        for (String token : fileName.split("_")) {
+        for (String token : pkgName.split("\\.")) {
+            if (token.isEmpty()) {
+                continue;
+            }
+            if (Character.isDigit(token.charAt(0))) {
+                token = "_" + token;
+            }
             namespace.add(token);
             headerFile.printf("namespace %s {\n", token);
             cppFile.printf("namespace %s {\n", token);
@@ -148,7 +157,7 @@
             cppFile.printf("} // %s\n", token);
         }
 
-        headerFile.printf("#endif // %s_H\n",fileName.toUpperCase().replace(".", "_"));
+        headerFile.printf("#endif // %s\n", headerMacro);
         cppFile.close();
         headerFile.close();
     }
@@ -436,7 +445,7 @@
                     + "}\n\n");
         }
 
-        String className = Utils.toClassName(fileName);
+        String className = Utils.toClassName(pkgName);
 
         boolean isMultiRootElement = xmlSchema.getElementMap().values().size() > 1;
         for (XsdElement element : xmlSchema.getElementMap().values()) {
diff --git a/src/com/android/xsdc/cpp/Utils.java b/src/com/android/xsdc/cpp/Utils.java
index b76a835..d225114 100644
--- a/src/com/android/xsdc/cpp/Utils.java
+++ b/src/com/android/xsdc/cpp/Utils.java
@@ -66,7 +66,7 @@
 
     static String toClassName(String name) throws CppCodeGeneratorException {
         String trimmed = toCamelCase(
-                name.replaceAll("[^A-Za-z0-9_-]", "").replaceAll("-","_").split("_"));
+                name.replaceAll("[^A-Za-z0-9_-]", "").replaceAll("[\\.-]", "_").split("_"));
         if (trimmed.isEmpty() || Character.isDigit(trimmed.charAt(0))) {
             throw new CppCodeGeneratorException(
                     String.format("cannot convert to a class name : %s", name));