TOPO DN 解析

介绍

    有一个算法,是将大量TOPO DN实例解析成结构形式,并根据DN获取相应的数据。本人感觉值得分享,并供大家讨论与优化。


    注意:TOPO DN实例的顺序,是被我的其它算法预处理过的,所以会比较规整。


TOPO DN实例输入片断:

"SubNetwork=HW-345678,ManagedElement=XXXX-1500/CSCF-1505",
"SubNetwork=HW-345678,ManagedElement=XXXX-1500/CSCF-1505,InventoryUnitRack=RACK-1",
"SubNetwork=HW-345678,ManagedElement=XXXX-1500/CSCF-1505,InventoryUnitRack=RACK-1,InventoryUnitShelf=RACK-1/SHELF-3",
"SubNetwork=HW-345678,ManagedElement=XXXX-1500/CSCF-1505,InventoryUnitRack=RACK-1,InventoryUnitShelf=RACK-1/SHELF-3,InventoryUnitPack=RACK-1/SHELF-3/UNIT-5",
"SubNetwork=HW-345678,ManagedElement=XXXX-1011/CSCF-1011",
"SubNetwork=HW-345678,ManagedElement=XXXX-1011/CSCF-1011,IcscfFunction=CSCF-1011",
"SubNetwork=HW-345678,ManagedElement=XXXX-1011/CSCF-1011,IcscfFunction=CSCF-1011,EpRpDynDnsIcscf=DNSPH-1",
"SubNetwork=HW-345678,ManagedElement=XXXX-1011/CSCF-1011,IcscfFunction=CSCF-1011,EpRpDynCxIcscf=DIA-1",
"SubNetwork=HW-345678,ManagedElement=XXXX-1011/CSCF-1011,IcscfFunction=CSCF-1011,EpRpDynDxIcscf=EPRPDX-1006",
"SubNetwork=HW-345678,ManagedElement=XXXX-1011/CSCF-1011,ScscfFunction=CSCF-1011",
"SubNetwork=HW-345678,ManagedElement=XXXX-1011/CSCF-1011,ScscfFunction=CSCF-1011,EpRpDynCxScscf=DIA-1"


TOPO DN实例预期输出片断:

<ManagedElement id="XXXX-1500/CSCF-1505">
	<InventoryUnitRack id="RACK-1">
		<!-- 在完整的代码中,这些地方会根据DN写入数据 -->
		<InventoryUnitShelf id="RACK-1/SHELF-3">
			<InventoryUnitPack id="RACK-1/SHELF-3/UNIT-5">
				<!-- 在完整的代码中,这些地方会根据DN写入数据 -->
			</InventoryUnitPack>
		</InventoryUnitShelf>
	</InventoryUnitRack>
</ManagedElement>
<ManagedElement id="XXXX-1011/CSCF-1011">
	<IcscfFunction id="CSCF-1011">
		<EpRpDynDnsIcscf id="DNSPH-1">
			......
		</EpRpDynDnsIcscf>
		<EpRpDynCxIcscf id="DIA-1">
			......
		</EpRpDynCxIcscf>
		<EpRpDynDxIcscf id="EPRPDX-1006">
			......
		</EpRpDynDxIcscf>
	</IcscfFunction>
	<ScscfFunction id="CSCF-1011">
		<EpRpDynCxScscf id="DIA-1">
			......
		</EpRpDynCxScscf>
	</ScscfFunction>
</ManagedElement>

核心代码片断:

package shuai.study.dnProcess;

import org.apache.commons.lang3.StringUtils;

/**
 * @author shengshu
 * 
 */
public class DnProcess {
	public static void process(String[] dnPathArray) {
		int dnPathLength = dnPathArray.length;

		for (int i = 0; i < dnPathLength; i++) {
			String currentDnPath = dnPathArray[i];
			String[] currentDnPathArray = StringUtils.split(currentDnPath, ",");
			int currentDnPathLength = currentDnPathArray.length;

			System.out.println("<" + StringUtils.substringBefore(currentDnPathArray[currentDnPathLength - 1], "=") + " id=\""
					+ StringUtils.substringAfter(currentDnPathArray[currentDnPathLength - 1], "=") + "\">");

			if (i == dnPathLength - 1) {
				for (int k = currentDnPathLength - 1; k > 0; k--) {
					System.out.println("</" + StringUtils.substringBefore(currentDnPathArray[k], "=") + ">");
				}
			} else {
				String nextDnPath = dnPathArray[i + 1];
				String[] nextDnPathArray = StringUtils.split(nextDnPath, ",");
				int nextDnPathLength = nextDnPathArray.length;

				if (currentDnPathLength == nextDnPathLength) {
					System.out.println("</" + StringUtils.substringBefore(currentDnPathArray[currentDnPathLength - 1], "=") + ">");
				}

				if (currentDnPathLength > nextDnPathLength) {
					for (int j = currentDnPathLength - 1; j >= nextDnPathLength - 1; j--) {
						System.out.println("</" + StringUtils.substringBefore(currentDnPathArray[j], "=") + ">");
					}
				}
			}
		}
	}

	public static void main(String[] args) {
		String[] dnPathArray = new String[] { "SubNetwork=HW-345678,ManagedElement=XXXX-1500/CSCF-1505",
				"SubNetwork=HW-345678,ManagedElement=XXXX-1500/CSCF-1505,InventoryUnitRack=RACK-1",
				"SubNetwork=HW-345678,ManagedElement=XXXX-1500/CSCF-1505,InventoryUnitRack=RACK-1,InventoryUnitShelf=RACK-1/SHELF-3",
				"SubNetwork=HW-345678,ManagedElement=XXXX-1500/CSCF-1505,InventoryUnitRack=RACK-1,InventoryUnitShelf=RACK-1/SHELF-3,InventoryUnitPack=RACK-1/SHELF-3/UNIT-5",
				"SubNetwork=HW-345678,ManagedElement=XXXX-1011/CSCF-1011", "SubNetwork=HW-345678,ManagedElement=XXXX-1011/CSCF-1011,IcscfFunction=CSCF-1011",
				"SubNetwork=HW-345678,ManagedElement=XXXX-1011/CSCF-1011,IcscfFunction=CSCF-1011,EpRpDynDnsIcscf=DNSPH-1",
				"SubNetwork=HW-345678,ManagedElement=XXXX-1011/CSCF-1011,IcscfFunction=CSCF-1011,EpRpDynCxIcscf=DIA-1",
				"SubNetwork=HW-345678,ManagedElement=XXXX-1011/CSCF-1011,IcscfFunction=CSCF-1011,EpRpDynDxIcscf=EPRPDX-1006",
				"SubNetwork=HW-345678,ManagedElement=XXXX-1011/CSCF-1011,ScscfFunction=CSCF-1011",
				"SubNetwork=HW-345678,ManagedElement=XXXX-1011/CSCF-1011,ScscfFunction=CSCF-1011,EpRpDynCxScscf=DIA-1" };

		DnProcess.process(dnPathArray);
	}
}




上一篇:【方法3:Perl版本】删除Map中Value重复的记录,并且只保留Key最小的那条记录


下一篇:CSV 文件解析