2. EMF鐢熸垚鐨勬柟娉曢兘甯︽湁“@generated”鏍囩銆?/span>EMF閲嶆柊鐢熸垚浠g爜鏃朵笉浼氳鐩栫敤鎴鋒坊鍔犵殑鏂規硶錛堜笉甯︽湁@generated錛夈?/span>
3. 鍏充簬URI錛?/span>Uniform Resource Identifier錛?/span>
鍦?/span>EMF 涓槸閫氳繃URI鏉ユ爣璇?/span>package鐨勶紝騫朵笖閫氳繃URI鏉ュ敮涓鐨勭‘瀹?/span>resources銆?/span>URI鍖呮嫭涓変釜閮ㄥ垎錛?/span>a scheme, a scheme-specific part鍜?/span>an optional fragment銆?/span>scheme涓昏鍐沖畾浜嗚闂?/span>
璧勬簮鐨勫崗璁紱姣斿錛?/span>Platform:/resource/……涓殑platform銆?/span>scheme-specific part鍖呭惈涓浜?/span>authority, device鍜屼竴浜?/span>segments錛屽file:/c:/dir1/dir2/myfile.xml錛?/span>file鏄崗璁紝娌℃湁authority錛?/span>c:鏄?/span>device,鍓╀笅鐨?/span>3涓槸segments銆?/span>URI fragment 鏍囪瘑浜?/span>resource鍐呴儴鐨勬煇涓叿浣撶殑鍐呭銆傚錛?/span>file:/c:/dir1/dir2/myfile.xml#loc涓殑#loc銆?/span> EMF閫氳繃甯︽湁fragment鐨?/span>URI鏉ヨ闂祫婧愪腑鐨?/span>EObjects銆?/span>
4. 鍏充簬URIConverter
URIConverter鐨勪綔鐢ㄦ槸normalize涓涓緭鍏?/span>URI錛屼嬌涔嬫垚涓轟竴涓疄闄呯殑閽堝鏌愪釜resource鐨?/span>URI銆傚畠鍙互鎶?/span>namespace URIs錛堟瘮濡傦細http:///com/example/epo2.ecore錛夋槧灝勫埌鐗╃悊鏂囦歡鐨?/span>URIs, 鎴栬呴噸瀹氬悜鏃х殑錛堟垨鍒悕錛夌殑URI鍙傝冨埌涓涓叿浣撶殑瀹為檯鐨?/span>location銆備竴涓?/span>URIConverter緇存姢涓涓?/span>URI鍒?/span>URI鐨勬槧灝勯泦鍚堛傛瘮濡傦紝鎶婁竴涓懡鍚嶇┖闂?/span>URI鏄犲皠鍒扮墿鐞嗘枃浠訛細
URIConverter converter = new URIConverterImpl();
URI uri1 = URI.createURI("http:///somemodel.ecore");
URI uri2 =URI.createURI("platform:/resource/project/somemodel.ecore");
converter.getURIMap().put(uri1, uri2);
鍦ㄥ涓嬮潰浠g爜錛?/span>
URI normalized = converter.normalize(uri1);
System.out.println(normalized);
鎵撳嵃鐨勭粨鏋滄槸錛?/span>platform:/resource/project/somemodel.ecore
URIConverter.normalize()鏂規硶鍙槸綆鍗曠殑閫氳繃鏄犲皠鐨?/span>map鎶?/span>key鏇挎崲鎴愪簡鐩稿簲鐨?/span>value銆?/span>
URIConverter鐨勬渶鍘熷鏄簲鐢ㄥ湪resource sets錛岀敤鏉ュ畾浣?/span>resources.
5. 鍏充簬Resource鍜?/span>ResourceSet
Resource 琛ㄧず涓涓寔涔呭寲鐨?/span>EOjbects鐨勫鍣紱ResourceSet琛ㄧず涓緇?/span>Resource鐨勯泦鍚堬紝闆嗗悎涓殑Resource鍚屾椂鍒涘緩鎴栧姞杞姐?/span>Resource涓瘮杈冮噸瑕佺殑灝辨槸save鍜?/span>load鏂規硶錛岃繕鏈夐氳繃URI fragments璁塊棶璧勬簮涓殑Object鐨勬満鍒訛紝濡傦細
Resource resource =…
Item item = (Item)resource.getEObject("http://@orders.0/@items.2");
Item item =…
String fragment = resource.getURIFragment(item);
涓婇潰浠g爜涓殑涓や釜鏂規硶錛?/span>getEObject閫氳繃甯︽湁fregment鐨?/span>URI鑾峰緱涓涓?/span>EObject錛屼笌涔嬬浉鍙嶇殑鏂規硶getURIFragment()閫氳繃EObject鑾峰緱鐩稿簲鐨?/span>fragment path銆?/span>
ResourceSet涓湁浜涢噸瑕佺殑鏂規硶錛?/span>
createResource()鍒涘緩涓涓┖鐨?/span>Resource錛?/span>
getResource()閫氳繃resource鐨?/span>URI鏉ュ垱寤?/span>Resource錛?/span>
getEObject()錛岄氳繃URI涓殑fregment鏉ヨ幏寰楀叿浣撶殑EObject瀵硅薄銆?/span>
6. 鍏充簬Resource.Factory
鐢ㄦ潵鍒涘緩Resource錛?/span>resource factory 瑕佹敞鍐屽埌Registry瀹炰緥涓備竴涓?/span>factory 鍙互閫氳繃澶氱鏂瑰紡鐨?/span>URIs鏉ユ敞鍐岋紝鍖呮嫭URI scheme鎴栬?/span>URI鐨?/span>extension銆傚湪鎻掍歡鏂瑰紡鐨勫簲鐢ㄤ腑錛岄氳繃鎵╁睍鐐圭殑鏂瑰紡鍦ㄦ彃浠跺姞杞界殑鏃跺欐敞鍐?/span>descriptor銆備笅闈㈡槸Resource鐨勬簮浠g爜
public interface Resource extends Notifier
{
interface Factory
{
Resource createResource(URI uri);
interface Descriptor
{
Factory createFactory();
}
interface Registry
{
Factory getFactory(URI uri);
Map getProtocolToFactoryMap();
String DEFAULT_EXTENSION = "*";
Map getExtensionToFactoryMap();
Registry INSTANCE = new ResourceFactoryRegistryImpl
();
}
}
}
涓嬮潰鏄?/span>Registry涓殑getFactory()鏂規硶鐨勭畻娉曪紙寮曠敤鍘熸枃錛夛細
1. Check for a factory in the protocolToFactoryMap, using
the scheme of the URI.
2. If nothing was found, check the extensionToFactoryMap
using the file extension of the URI.
3. If still nothing was found, check the
extensionToFactoryMap using the DEFAULT_EXTENSION
(that is, the wildcard character "*").
4. If no extension match was found, call the
delegatedGetFactory() method. This allows you to supply
your own factory registry, with its own lookup
criteria.
5. If a descriptor was found, instead of an actual
factory, call the createFactory() method on the
descriptor to create the factory.
6. Finally, return the factory if one was found, or null.
tip錛?/span>emf緙虹渷鐨勫簭鍒楀寲鏂瑰紡鏄?/span>XMI銆傚洜姝わ紝濡傛灉娌℃湁鎵懼埌鐩稿簲娉ㄥ唽鐨?/span>factory緙虹渷鐨勫氨浼氳繑鍥炰互*娉ㄥ唽鐨勭己鐪佺殑factory錛岃繖涓?/span>factory鏄拡瀵?/span>XMI鐨?/span>factory錛屽嵆XMIResourceFactoryImpl銆傚錛屽浜?/span>XMIResourceFactoryImpl鐨勬墿灞曠偣澹版槑:
<extension point = "org.eclipse.emf.ecore.extension_parser">
<parser type="*" class="org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl"/>
</extension>
褰撻潪鎻掍歡搴旂敤鐨勬椂鍊欙紝鍙互閫氳繃鎵嬪伐鐨勬柟寮忔潵娉ㄥ唽factory錛屽錛?/span>
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put
("*", new XMIResourceFactoryImpl());
7. Resource閫氳繃涓ょ鏂瑰紡瀹炵幇XML鍜?/span>XMI銆?/span>
1) XML
瀹炵幇Resource瀛樺偍鐨勯噸瑕佹柟娉曟槸save, 鍙湁鍦ㄦ柟娉?/span>eIsSet()榪斿洖true鏃訛紝鐩稿叧鐨勫睘鎬у拰寮曠敤鎵嶄細琚簭鍒楀寲鍒?/span>xml涓傚浜庡瓨鍌ㄧ殑xml鏂囦歡鐨勬牸寮忓彲浠ラ氳繃璁劇疆OPTION_XML_MAP灞炴ф潵瀹屾垚錛屽涓嬮潰鍘熸枃錛?/span>
There are several reasons why you might want to use OPTION_XML_MAP when saving a resource:
1. You want to save your data so that it conforms to a particular XML Schema.
2. You have changed your model, and you want the resource to produce XML documents that will work with the old version of the model.
3. You want to design your own XML format without changing your model.
2) XMI
XMI鍜?/span>XML鍩烘湰鐩稿悓錛?/span>XMIResourceImpl灝辨槸
XMLResourceImpl鐨勭畝鍗曠戶鎵匡紝鍙槸鍦ㄥ紩鐢?/span>objects鏃朵嬌鐢?/span>XMI IDs